Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions src/runtime/content/exec_script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

是不是应该只针对后台脚本进行处理,原来的脚本逻辑最好不动

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);
Comment on lines +59 to +78
}
if (scriptRes.grantMap.none) {
// 不注入任何GM api
Expand All @@ -70,7 +90,8 @@ export default class ExecScript {
this.proxyContent = proxyContext(
global,
this.sandboxContent,
thisContext
thisContext,
isSandbox
);
}
}
Expand Down
19 changes: 18 additions & 1 deletion src/runtime/content/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
// 处理某些特殊的属性
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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)) {
Expand Down
Loading