diff --git a/src/runtime/content/exec_script.ts b/src/runtime/content/exec_script.ts index 4894317b4..91c17ac31 100644 --- a/src/runtime/content/exec_script.ts +++ b/src/runtime/content/exec_script.ts @@ -51,11 +51,31 @@ export default class ExecScript { }); this.GM_info = GMApi.GM_info(this.scriptRes); this.proxyMessage = new ProxyMessageManager(message); + let isSandbox = false; if (scriptFunc) { this.scriptFunc = scriptFunc; } else { // 构建脚本资源 - this.scriptFunc = compileScript(this.scriptRes.code); + const scriptType = this.scriptRes.type; + let scriptCode = this.scriptRes.code; + if ((scriptType === 2 || scriptType === 3) && scriptCode.length > 0) { + isSandbox = true; + // background script / scheduled script + // we need to remove the access to chrome or browser in FirefoxMV2 + // since sandbox manifest support is still missing. + const arr = scriptCode.split("\n"); + for (let i = 0, l = arr.length; i < l; i++) { + const t = arr[i].trim(); + if (!t || t.startsWith("//")) continue; + const codeGen = (target: string, property: string, value: string): string => { + return `try { Object.defineProperty(${target}, "${property}", { get() { return ${value}; }, configurable: false }); } catch {}`; + }; + arr.splice(i, 0, `let chrome = {}, browser = undefined, frameElement = null, parent = window, top = window; try { window.parent = window; } catch {} ${codeGen("window", "parent", "this")} ${codeGen("window", "top", "this")} ${codeGen("window", "chrome", "{}")} ${codeGen("window", "browser", "undefined")} ${codeGen("window", "frameElement", "null")}`) + scriptCode = arr.join("\n"); + break; + } + } + this.scriptFunc = compileScript(scriptCode); } if (scriptRes.grantMap.none) { // 不注入任何GM api @@ -70,7 +90,8 @@ export default class ExecScript { this.proxyContent = proxyContext( global, this.sandboxContent, - thisContext + thisContext, + isSandbox ); } } diff --git a/src/runtime/content/utils.ts b/src/runtime/content/utils.ts index 00024a039..139db343f 100644 --- a/src/runtime/content/utils.ts +++ b/src/runtime/content/utils.ts @@ -93,7 +93,8 @@ export function warpObject(thisContext: Object, ...context: Object[]) { export function proxyContext( global: any, context: any, - thisContext?: { [key: string]: any } + thisContext?: { [key: string]: any }, + isSandbox: boolean = false ) { const special = Object.assign(writables); // 处理某些特殊的属性 @@ -131,10 +132,15 @@ export function proxyContext( return proxy; case "top": case "parent": + if (isSandbox) return proxy; if (global[name] === global.self) { return special.global || proxy; } return global[name]; + // eslint-disable-next-line no-fallthrough + case "frameElement": + if (isSandbox) return null; + // eslint-disable-next-line no-fallthrough default: break; } @@ -195,6 +201,10 @@ export function proxyContext( case "top": case "parent": return true; + // eslint-disable-next-line no-fallthrough + case "frameElement": + if (isSandbox) return true; + // eslint-disable-next-line no-fallthrough default: break; } @@ -233,6 +243,13 @@ export function proxyContext( case "self": case "globalThis": return false; + case "top": + case "parent": + if (isSandbox) return false; + // eslint-disable-next-line no-fallthrough + case "frameElement": + if (isSandbox) return false; + // eslint-disable-next-line no-fallthrough default: } if (has(special, name)) {