Skip to content

Commit a2e0a74

Browse files
committed
GM API 定义修正
1 parent bea0192 commit a2e0a74

4 files changed

Lines changed: 214 additions & 111 deletions

File tree

src/app/service/content/content.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ export default class ContentRuntime {
7676
case "GM_addElement": {
7777
const [parentNodeId, tagName, tmpAttr] = data.params;
7878
let attr = { ...tmpAttr };
79-
let parentNode: EventTarget | undefined;
79+
let parentNode: Node | undefined;
8080
// 判断是不是content脚本发过来的
8181
let msg: CustomEventMessage;
8282
if (this.contentScriptSet.has(data.uuid) || this.scriptExecutor.execMap.has(data.uuid)) {
@@ -85,7 +85,7 @@ export default class ContentRuntime {
8585
msg = this.senderToInject;
8686
}
8787
if (parentNodeId) {
88-
parentNode = msg.getAndDelRelatedTarget(parentNodeId);
88+
parentNode = msg.getAndDelRelatedTarget(parentNodeId) as Node | undefined;
8989
}
9090
const el = <Element>document.createElement(tagName);
9191

@@ -104,7 +104,7 @@ export default class ContentRuntime {
104104
if (textContent) {
105105
el.textContent = textContent;
106106
}
107-
(<Element>parentNode || document.head || document.body || document.querySelector("*")).appendChild(el);
107+
(parentNode || document.head || document.body || document.querySelector("*")).appendChild(el);
108108
const nodeId = msg.sendRelatedTarget(el);
109109
return nodeId;
110110
}

src/app/service/content/gm_api/gm_api.test.ts

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -33,31 +33,38 @@ const envInfo: GMInfoEnv = {
3333
describe.concurrent("@grant GM", () => {
3434
it.concurrent("GM_", async () => {
3535
const script = Object.assign({}, scriptRes) as ScriptLoadInfo;
36-
script.metadata.grant = ["GM_getValue", "GM_getTab", "GM_saveTab", "GM_cookie"];
36+
script.metadata.grant = ["GM_getValue", "GM_getTab", "GM_getTabs", "GM_saveTab", "GM_cookie"];
3737
// @ts-ignore
3838
const exec = new ExecScript(script, undefined, undefined, nilFn, envInfo);
3939
script.code = `return {
4040
["GM.getValue"]: GM.getValue,
4141
["GM.getTab"]: GM.getTab,
42-
["GM.setTab"]: GM.setTab,
42+
["GM.getTabs"]: GM.getTabs,
43+
["GM.saveTab"]: GM.saveTab,
4344
GM_getValue: this.GM_getValue,
4445
GM_getTab: this.GM_getTab,
46+
GM_getTabs: this.GM_getTabs,
4547
GM_saveTab: this.GM_saveTab,
4648
GM_cookie: this.GM_cookie,
4749
["GM_cookie.list"]: this.GM_cookie.list,
4850
["GM.cookie"]: this.GM.cookie,
4951
}`;
5052
exec.scriptFunc = compileScript(compileScriptCode(script));
5153
const ret = await exec.exec();
52-
expect(ret["GM.getValue"]).toBeUndefined();
53-
expect(ret["GM.getTab"]).toBeUndefined();
54-
expect(ret["GM.setTab"]).toBeUndefined();
55-
expect(ret.GM_getValue.name).toEqual("bound GM_getValue");
56-
expect(ret.GM_getTab.name).toEqual("bound GM_getTab");
57-
expect(ret.GM_saveTab.name).toEqual("bound GM_saveTab");
58-
expect(ret.GM_cookie.name).toEqual("bound GM_cookie");
59-
expect(ret["GM_cookie.list"].name).toEqual("bound GM_cookie.list");
60-
expect(ret["GM.cookie"]).toBeUndefined();
54+
// getValue
55+
expect(ret.GM_getValue?.name).toEqual("bound GM_getValue");
56+
expect(ret["GM.getValue"]?.name).toBeUndefined();
57+
// getTab / getTabs / saveTab
58+
expect(ret.GM_getTab?.name).toEqual("bound GM_getTab");
59+
expect(ret.GM_getTabs?.name).toEqual("bound GM_getTabs");
60+
expect(ret.GM_saveTab?.name).toEqual("bound GM_saveTab");
61+
expect(ret["GM.getTab"]?.name).toBeUndefined();
62+
expect(ret["GM.getTabs"]?.name).toBeUndefined();
63+
expect(ret["GM.saveTab"]?.name).toBeUndefined();
64+
// cookie
65+
expect(ret.GM_cookie?.name).toEqual("bound GM_cookie");
66+
expect(ret["GM_cookie.list"]?.name).toEqual("bound GM_cookie.list");
67+
expect(ret["GM.cookie"]?.name).toBeUndefined();
6168
});
6269

6370
it.concurrent("GM.*", async () => {
@@ -79,17 +86,20 @@ describe.concurrent("@grant GM", () => {
7986
}`;
8087
exec.scriptFunc = compileScript(compileScriptCode(script));
8188
const ret = await exec.exec();
82-
expect(ret["GM.getValue"].name).toEqual("bound GM.getValue");
83-
expect(ret["GM.getTab"].name).toEqual("bound GM.getTab");
84-
expect(ret["GM.getTabs"].name).toEqual("bound GM.getTabs");
85-
expect(ret["GM.saveTab"].name).toEqual("bound GM_saveTab");
86-
expect(ret.GM_getValue).toBeUndefined();
87-
expect(ret.GM_getTab.name).toEqual("bound GM_getTab");
88-
expect(ret.GM_getTabs.name).toEqual("bound GM_getTabs");
89-
expect(ret.GM_saveTab).toBeUndefined();
90-
expect(ret.GM_cookie).toBeUndefined();
91-
expect(ret["GM.cookie"].name).toEqual("bound GM.cookie");
92-
expect(ret["GM.cookie"].list.name).toEqual("bound GM.cookie.list");
89+
// getValue
90+
expect(ret["GM.getValue"]?.name).toEqual("bound GM.getValue");
91+
expect(ret.GM_getValue?.name).toBeUndefined();
92+
// getTab / getTabs / saveTab
93+
// expect(ret.GM_getTab?.name).toBeUndefined();
94+
// expect(ret.GM_getTabs?.name).toBeUndefined();
95+
// expect(ret.GM_saveTab?.name).toBeUndefined();
96+
expect(ret["GM.getTab"]?.name).toEqual("bound GM.getTab");
97+
expect(ret["GM.getTabs"]?.name).toEqual("bound GM.getTabs");
98+
expect(ret["GM.saveTab"]?.name).toEqual("bound GM.saveTab");
99+
// cookie
100+
expect(ret.GM_cookie?.name).toBeUndefined();
101+
expect(ret["GM.cookie"]?.name).toEqual("bound GM.cookie");
102+
expect(ret["GM.cookie"]?.list?.name).toEqual("bound GM.cookie.list");
93103
});
94104
});
95105

0 commit comments

Comments
 (0)