From 416ab281c72e8060d01e7b2f310a224db4e4b598 Mon Sep 17 00:00:00 2001 From: DreamNya <34838824+DreamNya@users.noreply.github.com> Date: Thu, 1 Feb 2024 12:24:35 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E8=84=9A=E6=9C=AC?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E9=A1=B6=E7=BA=A7await?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/runtime/content/utils.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/runtime/content/utils.ts b/src/runtime/content/utils.ts index e66f86ba..99397073 100644 --- a/src/runtime/content/utils.ts +++ b/src/runtime/content/utils.ts @@ -17,7 +17,7 @@ export function compileScriptCode(scriptRes: ScriptRunResouce): string { }); } code = require + code; - return `with (context) return (()=>{\n${code}\n//# sourceURL=${chrome.runtime.getURL( + return `with (context) return (async ()=>{\n${code}\n//# sourceURL=${chrome.runtime.getURL( `/${encodeURI(scriptRes.name)}.user.js` )}\n})()`; } From 2bc732bdd9f47fd8fd7d976555e93849e31ef961 Mon Sep 17 00:00:00 2001 From: DreamNya <34838824+DreamNya@users.noreply.github.com> Date: Thu, 1 Feb 2024 12:55:56 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E5=BC=82=E6=AD=A5Jest?= =?UTF-8?q?=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/runtime/content/exec_script.test.ts | 30 ++++++++++++------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/src/runtime/content/exec_script.test.ts b/src/runtime/content/exec_script.test.ts index 07067039..dd3a7670 100644 --- a/src/runtime/content/exec_script.test.ts +++ b/src/runtime/content/exec_script.test.ts @@ -39,51 +39,51 @@ const scriptRes2 = { const sandboxExec = new ExecScript(scriptRes2); describe("GM_info", () => { - it("none", () => { + it("none", async () => { scriptRes.code = "return GM_info"; noneExec.scriptFunc = compileScript(compileScriptCode(scriptRes)); - const ret = noneExec.exec(); + const ret = await noneExec.exec(); expect(ret.version).toEqual(ExtVersion); expect(ret.script.version).toEqual("1.0.0"); }); - it("sandbox", () => { + it("sandbox", async() => { scriptRes2.code = "return GM_info"; sandboxExec.scriptFunc = compileScript(compileScriptCode(scriptRes2)); - const ret = sandboxExec.exec(); + const ret = await sandboxExec.exec(); expect(ret.version).toEqual(ExtVersion); expect(ret.script.version).toEqual("1.0.0"); }); }); describe("unsafeWindow", () => { - it("sandbox", () => { + it("sandbox", async () => { // @ts-ignore global.testUnsafeWindow = "ok"; scriptRes2.code = "return unsafeWindow.testUnsafeWindow"; sandboxExec.scriptFunc = compileScript(compileScriptCode(scriptRes2)); - const ret = sandboxExec.exec(); + const ret = await sandboxExec.exec(); expect(ret).toEqual("ok"); }); }); describe("sandbox", () => { - it("global", () => { + it("global", async () => { scriptRes2.code = "window.testObj = 'ok';return window.testObj"; sandboxExec.scriptFunc = compileScript(compileScriptCode(scriptRes2)); - let ret = sandboxExec.exec(); + let ret = await sandboxExec.exec(); expect(ret).toEqual("ok"); scriptRes2.code = "window.testObj = 'ok2';return testObj"; sandboxExec.scriptFunc = compileScript(compileScriptCode(scriptRes2)); - ret = sandboxExec.exec(); + ret = await sandboxExec.exec(); expect(ret).toEqual("ok2"); }); - it("this", () => { + it("this", async () => { scriptRes2.code = "this.testObj='ok2';return testObj;"; sandboxExec.scriptFunc = compileScript(compileScriptCode(scriptRes2)); - const ret = sandboxExec.exec(); + const ret = await sandboxExec.exec(); expect(ret).toEqual("ok2"); }); - it("this2", () => { + it("this2", async () => { scriptRes2.code = ` !function(t, e) { "object" == typeof exports ? module.exports = exports = e() : "function" == typeof define && define.amd ? define([], e) : t.CryptoJS = e() @@ -94,15 +94,15 @@ describe("sandbox", () => { console.log(CryptoJS) return CryptoJS.test;`; sandboxExec.scriptFunc = compileScript(compileScriptCode(scriptRes2)); - const ret = sandboxExec.exec(); + const ret = await sandboxExec.exec(); expect(ret).toEqual("ok3"); }); // 沉浸式翻译, 常量值被改变 - it("NodeFilter #214", () => { + it("NodeFilter #214", async () => { scriptRes2.code = `return NodeFilter.FILTER_REJECT;`; sandboxExec.scriptFunc = compileScript(compileScriptCode(scriptRes2)); - const ret = sandboxExec.exec(); + const ret = await sandboxExec.exec(); expect(ret).toEqual(2); }); });