|
| 1 | +import { ExternalWhitelist } from "@App/app/const"; |
| 2 | +import { sendMessage } from "@Packages/message/client"; |
| 3 | +import type { Message } from "@Packages/message/types"; |
| 4 | + |
| 5 | +// ================================ |
| 6 | +// 对外接口:external 注入 |
| 7 | +// ================================ |
| 8 | + |
| 9 | +// 判断当前 hostname 是否命中白名单(含子域名) |
| 10 | +const isExternalWhitelisted = (hostname: string) => { |
| 11 | + return ExternalWhitelist.some( |
| 12 | + (t) => hostname.endsWith(t) && (hostname.length === t.length || hostname.endsWith(`.${t}`)) |
| 13 | + ); |
| 14 | +}; |
| 15 | + |
| 16 | +// 生成暴露给页面的 Scriptcat 外部接口 |
| 17 | +const createScriptcatExpose = (msg: Message) => { |
| 18 | + const scriptExpose: App.ExternalScriptCat = { |
| 19 | + isInstalled(name: string, namespace: string, callback: (res: App.IsInstalledResponse | undefined) => unknown) { |
| 20 | + sendMessage<App.IsInstalledResponse>(msg, "scripting/script/isInstalled", { name, namespace }).then(callback); |
| 21 | + }, |
| 22 | + }; |
| 23 | + return scriptExpose; |
| 24 | +}; |
| 25 | + |
| 26 | +// 尝试写入 external,失败则忽略 |
| 27 | +const safeSetExternal = <T extends object>(external: any, key: string, value: T) => { |
| 28 | + try { |
| 29 | + external[key] = value; |
| 30 | + return true; |
| 31 | + } catch { |
| 32 | + // 无法注入到 external,忽略 |
| 33 | + return false; |
| 34 | + } |
| 35 | +}; |
| 36 | + |
| 37 | +// 当 TM 与 SC 同时存在时的兼容处理:TM 未安装脚本时回退查询 SC |
| 38 | +const patchTampermonkeyIsInstalled = (external: any, scriptExpose: App.ExternalScriptCat) => { |
| 39 | + const exposedTM = external.Tampermonkey; |
| 40 | + const isInstalledTM = exposedTM?.isInstalled; |
| 41 | + const isInstalledSC = scriptExpose.isInstalled; |
| 42 | + |
| 43 | + // 满足这些字段时,认为是较完整的 TM 对象 |
| 44 | + if (isInstalledTM && exposedTM?.getVersion && exposedTM.openOptions) { |
| 45 | + try { |
| 46 | + exposedTM.isInstalled = ( |
| 47 | + name: string, |
| 48 | + namespace: string, |
| 49 | + callback: (res: App.IsInstalledResponse | undefined) => unknown |
| 50 | + ) => { |
| 51 | + isInstalledTM(name, namespace, (res: App.IsInstalledResponse | undefined) => { |
| 52 | + if (res?.installed) callback(res); |
| 53 | + else isInstalledSC(name, namespace, callback); |
| 54 | + }); |
| 55 | + }; |
| 56 | + } catch { |
| 57 | + // 忽略错误 |
| 58 | + } |
| 59 | + return true; |
| 60 | + } |
| 61 | + |
| 62 | + return false; |
| 63 | +}; |
| 64 | + |
| 65 | +// inject 环境 pageLoad 后执行:按白名单对页面注入 external 接口 |
| 66 | +export const onInjectPageLoaded = (msg: Message) => { |
| 67 | + const hostname = window.location.hostname; |
| 68 | + |
| 69 | + // 不在白名单则不对外暴露接口 |
| 70 | + if (!isExternalWhitelisted(hostname)) return; |
| 71 | + |
| 72 | + // 确保 external 存在 |
| 73 | + const external: External = (window.external || (window.external = {} as External)) as External; |
| 74 | + |
| 75 | + // 创建 Scriptcat 暴露对象 |
| 76 | + const scriptExpose = createScriptcatExpose(msg); |
| 77 | + |
| 78 | + // 尝试设置 external.Scriptcat |
| 79 | + safeSetExternal(external, "Scriptcat", scriptExpose); |
| 80 | + |
| 81 | + // 如果页面已有 Tampermonkey,则做兼容补丁;否则将 Tampermonkey 也指向 Scriptcat 接口 |
| 82 | + const patched = patchTampermonkeyIsInstalled(external, scriptExpose); |
| 83 | + if (!patched) { |
| 84 | + safeSetExternal(external, "Tampermonkey", scriptExpose); |
| 85 | + } |
| 86 | +}; |
0 commit comments