Skip to content

Commit

Permalink
🐛 新增脚本支持顶级await (#258)
Browse files Browse the repository at this point in the history
* 新增脚本支持顶级await

* 更新异步Jest测试
  • Loading branch information
DreamNya committed Feb 1, 2024
1 parent 061e285 commit 3a37af2
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 16 deletions.
30 changes: 15 additions & 15 deletions src/runtime/content/exec_script.test.ts
Expand Up @@ -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()
Expand All @@ -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);
});
});
2 changes: 1 addition & 1 deletion src/runtime/content/utils.ts
Expand Up @@ -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})()`;
}
Expand Down

0 comments on commit 3a37af2

Please sign in to comment.