Skip to content

Commit

Permalink
🐛 修复沙盒变量undefined问题
Browse files Browse the repository at this point in the history
  • Loading branch information
CodFrm committed Mar 3, 2023
1 parent f9cb6dc commit 2dcf9c2
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 10 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "scriptcat",
"version": "0.11.2",
"version": "0.11.3",
"description": "脚本猫,一个可以执行用户脚本的浏览器扩展,万物皆可脚本化,让你的浏览器可以做更多的事情!",
"author": "CodFrm",
"license": "GPLv3",
Expand Down
2 changes: 1 addition & 1 deletion src/app/const.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export const ExtVersion = "0.11.2";
export const ExtVersion = "0.11.3";

export const ExtServer = "https://ext.scriptcat.org/";

Expand Down
2 changes: 1 addition & 1 deletion src/manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"manifest_version": 2,
"name": "ScriptCat",
"version": "0.11.2",
"version": "0.11.3",
"author": "CodFrm",
"description": "脚本猫,一个用户脚本管理器,支持后台脚本、定时脚本、页面脚本,可编写脚本每天帮你自动处理事务.",
"options_ui": {
Expand Down
9 changes: 5 additions & 4 deletions src/runtime/content/exec_script.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import initTestEnv from "@App/pkg/utils/test_utils";
import { ScriptRunResouce } from "@App/app/repo/scripts";
import ExecScript from "./exec_script";
import { compileScript, compileScriptCode } from "./utils";
import { ExtVersion } from "@App/app/const";

initTestEnv();

Expand Down Expand Up @@ -42,15 +43,15 @@ describe("GM_info", () => {
scriptRes.code = "return GM_info";
noneExec.scriptFunc = compileScript(compileScriptCode(scriptRes));
const ret = noneExec.exec();
expect(ret.version).toEqual("1.0.0");
expect(ret.scriptSource).toEqual("sourceCode");
expect(ret.version).toEqual(ExtVersion);
expect(ret.script.version).toEqual("1.0.0");
});
it("sandbox", () => {
scriptRes2.code = "return GM_info";
sandboxExec.scriptFunc = compileScript(compileScriptCode(scriptRes2));
const ret = sandboxExec.exec();
expect(ret.version).toEqual("1.0.0");
expect(ret.scriptSource).toEqual("sourceCode");
expect(ret.version).toEqual(ExtVersion);
expect(ret.script.version).toEqual("1.0.0");
});
});

Expand Down
53 changes: 50 additions & 3 deletions src/runtime/content/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ export function proxyContext(global: any, context: any) {
}
return false;
},
get(_, name) {
get(_, name): any {
switch (name) {
case "window":
case "self":
Expand Down Expand Up @@ -194,8 +194,55 @@ export function proxyContext(global: any, context: any) {
}
return undefined;
},
has() {
return true;
has(_, name) {
switch (name) {
case "window":
case "self":
case "globalThis":
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return true;
case "top":
case "parent":
if (global[name] === global.self) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return true;
}
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return true;
default:
break;
}
if (typeof name === "string" && name !== "undefined") {
if (context[name]) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return true;
}
if (thisContext[name]) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return true;
}
if (special[name] !== undefined) {
if (
typeof special[name] === "function" &&
!(<{ prototype: any }>special[name]).prototype
) {
return true;
}
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return true;
}
if (global[name] !== undefined) {
if (
typeof global[name] === "function" &&
!(<{ prototype: any }>global[name]).prototype
) {
return true;
}
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return true;
}
}
return false;
},
set(_, name: string, val) {
switch (name) {
Expand Down

0 comments on commit 2dcf9c2

Please sign in to comment.