Skip to content

Commit

Permalink
🐛 修复沙盒模式无法从window读取GM_info的问题 #126
Browse files Browse the repository at this point in the history
  • Loading branch information
CodFrm committed Dec 15, 2022
1 parent e0a0d64 commit f23ccde
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
12 changes: 10 additions & 2 deletions src/runtime/content/exec_script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ export default class ExecScript {

proxyMessage: ProxyMessageManager;

// eslint-disable-next-line camelcase
GM_info: any;

constructor(
scriptRes: ScriptRunResouce,
message: MessageManager,
Expand All @@ -45,6 +48,7 @@ export default class ExecScript {
id: this.scriptRes.id,
name: this.scriptRes.name,
});
this.GM_info = GMApi.GM_info(this.scriptRes);
this.proxyMessage = new ProxyMessageManager(message);
if (scriptFunc) {
this.scriptFunc = scriptFunc;
Expand All @@ -57,7 +61,11 @@ export default class ExecScript {
this.proxyContent = window;
} else {
// 构建脚本GM上下文
this.sandboxContent = createContext(scriptRes, this.proxyMessage);
this.sandboxContent = createContext(
scriptRes,
this.GM_info,
this.proxyMessage
);
this.proxyContent = proxyContext(window, this.sandboxContent);
}
}
Expand All @@ -71,7 +79,7 @@ export default class ExecScript {
this.logger.debug("script start");
return this.scriptFunc.apply(this.proxyContent, [
this.proxyContent,
GMApi.GM_info(this.scriptRes),
this.GM_info,
]);
}

Expand Down
7 changes: 3 additions & 4 deletions src/runtime/content/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ function setDepend(context: { [key: string]: any }, apiVal: ApiValue) {
// 构建沙盒上下文
export function createContext(
scriptRes: ScriptRunResouce,
GMInfo: any,
message: MessageManager
): GMApi {
// 按照GMApi构建
Expand All @@ -70,7 +71,8 @@ export function createContext(
connect: GMApi.prototype.connect,
runFlag: uuidv4(),
valueUpdate: GMApi.prototype.valueUpdate,
GM: {},
GM: { Info: GMInfo },
GM_info: GMInfo,
};
if (scriptRes.metadata.grant) {
scriptRes.metadata.grant.forEach((val) => {
Expand Down Expand Up @@ -193,9 +195,6 @@ export function proxyContext(global: any, context: any) {
return undefined;
},
has(_, name) {
if (name === "GM_info") {
return false;
}
return true;
},
set(_, name: string, val) {
Expand Down

0 comments on commit f23ccde

Please sign in to comment.