Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

新增脚本支持顶级await #258

Merged
merged 2 commits into from Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我有个问题,这样相当于把脚本注入做成了异步,在比较严格要求document.start的脚本中会不会出现问题?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

脚本注入是在window上挂载一个闭包函数,然后执行。
这段代码外部还有一个闭包函数,这段代码只是在闭包中把同步改成异步,不影响闭包外代码。

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我就是指的闭包内的这个代码,整个闭包相当于成为了异步,执行的优先级被拉低了,变成了微任务队列

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image
我试了下没啥问题

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

确实没问题,感觉我得复习下Js基础了

`/${encodeURI(scriptRes.name)}.user.js`
)}\n})()`;
}
Expand Down