Skip to content

Commit 3a37af2

Browse files
authored
🐛 新增脚本支持顶级await (#258)
* 新增脚本支持顶级await * 更新异步Jest测试
1 parent 061e285 commit 3a37af2

File tree

2 files changed

+16
-16
lines changed

2 files changed

+16
-16
lines changed

src/runtime/content/exec_script.test.ts

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -39,51 +39,51 @@ const scriptRes2 = {
3939
const sandboxExec = new ExecScript(scriptRes2);
4040

4141
describe("GM_info", () => {
42-
it("none", () => {
42+
it("none", async () => {
4343
scriptRes.code = "return GM_info";
4444
noneExec.scriptFunc = compileScript(compileScriptCode(scriptRes));
45-
const ret = noneExec.exec();
45+
const ret = await noneExec.exec();
4646
expect(ret.version).toEqual(ExtVersion);
4747
expect(ret.script.version).toEqual("1.0.0");
4848
});
49-
it("sandbox", () => {
49+
it("sandbox", async() => {
5050
scriptRes2.code = "return GM_info";
5151
sandboxExec.scriptFunc = compileScript(compileScriptCode(scriptRes2));
52-
const ret = sandboxExec.exec();
52+
const ret = await sandboxExec.exec();
5353
expect(ret.version).toEqual(ExtVersion);
5454
expect(ret.script.version).toEqual("1.0.0");
5555
});
5656
});
5757

5858
describe("unsafeWindow", () => {
59-
it("sandbox", () => {
59+
it("sandbox", async () => {
6060
// @ts-ignore
6161
global.testUnsafeWindow = "ok";
6262
scriptRes2.code = "return unsafeWindow.testUnsafeWindow";
6363
sandboxExec.scriptFunc = compileScript(compileScriptCode(scriptRes2));
64-
const ret = sandboxExec.exec();
64+
const ret = await sandboxExec.exec();
6565
expect(ret).toEqual("ok");
6666
});
6767
});
6868

6969
describe("sandbox", () => {
70-
it("global", () => {
70+
it("global", async () => {
7171
scriptRes2.code = "window.testObj = 'ok';return window.testObj";
7272
sandboxExec.scriptFunc = compileScript(compileScriptCode(scriptRes2));
73-
let ret = sandboxExec.exec();
73+
let ret = await sandboxExec.exec();
7474
expect(ret).toEqual("ok");
7575
scriptRes2.code = "window.testObj = 'ok2';return testObj";
7676
sandboxExec.scriptFunc = compileScript(compileScriptCode(scriptRes2));
77-
ret = sandboxExec.exec();
77+
ret = await sandboxExec.exec();
7878
expect(ret).toEqual("ok2");
7979
});
80-
it("this", () => {
80+
it("this", async () => {
8181
scriptRes2.code = "this.testObj='ok2';return testObj;";
8282
sandboxExec.scriptFunc = compileScript(compileScriptCode(scriptRes2));
83-
const ret = sandboxExec.exec();
83+
const ret = await sandboxExec.exec();
8484
expect(ret).toEqual("ok2");
8585
});
86-
it("this2", () => {
86+
it("this2", async () => {
8787
scriptRes2.code = `
8888
!function(t, e) {
8989
"object" == typeof exports ? module.exports = exports = e() : "function" == typeof define && define.amd ? define([], e) : t.CryptoJS = e()
@@ -94,15 +94,15 @@ describe("sandbox", () => {
9494
console.log(CryptoJS)
9595
return CryptoJS.test;`;
9696
sandboxExec.scriptFunc = compileScript(compileScriptCode(scriptRes2));
97-
const ret = sandboxExec.exec();
97+
const ret = await sandboxExec.exec();
9898
expect(ret).toEqual("ok3");
9999
});
100100

101101
// 沉浸式翻译, 常量值被改变
102-
it("NodeFilter #214", () => {
102+
it("NodeFilter #214", async () => {
103103
scriptRes2.code = `return NodeFilter.FILTER_REJECT;`;
104104
sandboxExec.scriptFunc = compileScript(compileScriptCode(scriptRes2));
105-
const ret = sandboxExec.exec();
105+
const ret = await sandboxExec.exec();
106106
expect(ret).toEqual(2);
107107
});
108108
});

src/runtime/content/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export function compileScriptCode(scriptRes: ScriptRunResouce): string {
1717
});
1818
}
1919
code = require + code;
20-
return `with (context) return (()=>{\n${code}\n//# sourceURL=${chrome.runtime.getURL(
20+
return `with (context) return (async ()=>{\n${code}\n//# sourceURL=${chrome.runtime.getURL(
2121
`/${encodeURI(scriptRes.name)}.user.js`
2222
)}\n})()`;
2323
}

0 commit comments

Comments
 (0)