Skip to content

Commit 2dcf9c2

Browse files
committed
🐛 修复沙盒变量undefined问题
1 parent f9cb6dc commit 2dcf9c2

5 files changed

Lines changed: 58 additions & 10 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "scriptcat",
3-
"version": "0.11.2",
3+
"version": "0.11.3",
44
"description": "脚本猫,一个可以执行用户脚本的浏览器扩展,万物皆可脚本化,让你的浏览器可以做更多的事情!",
55
"author": "CodFrm",
66
"license": "GPLv3",

src/app/const.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export const ExtVersion = "0.11.2";
1+
export const ExtVersion = "0.11.3";
22

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

src/manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"manifest_version": 2,
33
"name": "ScriptCat",
4-
"version": "0.11.2",
4+
"version": "0.11.3",
55
"author": "CodFrm",
66
"description": "脚本猫,一个用户脚本管理器,支持后台脚本、定时脚本、页面脚本,可编写脚本每天帮你自动处理事务.",
77
"options_ui": {

src/runtime/content/exec_script.test.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import initTestEnv from "@App/pkg/utils/test_utils";
22
import { ScriptRunResouce } from "@App/app/repo/scripts";
33
import ExecScript from "./exec_script";
44
import { compileScript, compileScriptCode } from "./utils";
5+
import { ExtVersion } from "@App/app/const";
56

67
initTestEnv();
78

@@ -42,15 +43,15 @@ describe("GM_info", () => {
4243
scriptRes.code = "return GM_info";
4344
noneExec.scriptFunc = compileScript(compileScriptCode(scriptRes));
4445
const ret = noneExec.exec();
45-
expect(ret.version).toEqual("1.0.0");
46-
expect(ret.scriptSource).toEqual("sourceCode");
46+
expect(ret.version).toEqual(ExtVersion);
47+
expect(ret.script.version).toEqual("1.0.0");
4748
});
4849
it("sandbox", () => {
4950
scriptRes2.code = "return GM_info";
5051
sandboxExec.scriptFunc = compileScript(compileScriptCode(scriptRes2));
5152
const ret = sandboxExec.exec();
52-
expect(ret.version).toEqual("1.0.0");
53-
expect(ret.scriptSource).toEqual("sourceCode");
53+
expect(ret.version).toEqual(ExtVersion);
54+
expect(ret.script.version).toEqual("1.0.0");
5455
});
5556
});
5657

src/runtime/content/utils.ts

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ export function proxyContext(global: any, context: any) {
144144
}
145145
return false;
146146
},
147-
get(_, name) {
147+
get(_, name): any {
148148
switch (name) {
149149
case "window":
150150
case "self":
@@ -194,8 +194,55 @@ export function proxyContext(global: any, context: any) {
194194
}
195195
return undefined;
196196
},
197-
has() {
198-
return true;
197+
has(_, name) {
198+
switch (name) {
199+
case "window":
200+
case "self":
201+
case "globalThis":
202+
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
203+
return true;
204+
case "top":
205+
case "parent":
206+
if (global[name] === global.self) {
207+
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
208+
return true;
209+
}
210+
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
211+
return true;
212+
default:
213+
break;
214+
}
215+
if (typeof name === "string" && name !== "undefined") {
216+
if (context[name]) {
217+
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
218+
return true;
219+
}
220+
if (thisContext[name]) {
221+
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
222+
return true;
223+
}
224+
if (special[name] !== undefined) {
225+
if (
226+
typeof special[name] === "function" &&
227+
!(<{ prototype: any }>special[name]).prototype
228+
) {
229+
return true;
230+
}
231+
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
232+
return true;
233+
}
234+
if (global[name] !== undefined) {
235+
if (
236+
typeof global[name] === "function" &&
237+
!(<{ prototype: any }>global[name]).prototype
238+
) {
239+
return true;
240+
}
241+
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
242+
return true;
243+
}
244+
}
245+
return false;
199246
},
200247
set(_, name: string, val) {
201248
switch (name) {

0 commit comments

Comments
 (0)