Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/app/service/content/content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export default class ContentRuntime {
case "GM_addElement": {
const [parentNodeId, tagName, tmpAttr] = data.params;
let attr = { ...tmpAttr };
let parentNode: EventTarget | undefined;
let parentNode: Node | undefined;
// 判断是不是content脚本发过来的
let msg: CustomEventMessage;
if (this.contentScriptSet.has(data.uuid) || this.scriptExecutor.execMap.has(data.uuid)) {
Expand All @@ -85,7 +85,7 @@ export default class ContentRuntime {
msg = this.senderToInject;
}
if (parentNodeId) {
parentNode = msg.getAndDelRelatedTarget(parentNodeId);
parentNode = msg.getAndDelRelatedTarget(parentNodeId) as Node | undefined;
}
const el = <Element>document.createElement(tagName);

Expand All @@ -104,7 +104,7 @@ export default class ContentRuntime {
if (textContent) {
el.textContent = textContent;
}
(<Element>parentNode || document.head || document.body || document.querySelector("*")).appendChild(el);
(parentNode || document.head || document.body || document.querySelector("*")).appendChild(el);
const nodeId = msg.sendRelatedTarget(el);
return nodeId;
}
Expand Down
54 changes: 32 additions & 22 deletions src/app/service/content/gm_api/gm_api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,31 +33,38 @@ const envInfo: GMInfoEnv = {
describe.concurrent("@grant GM", () => {
it.concurrent("GM_", async () => {
const script = Object.assign({}, scriptRes) as ScriptLoadInfo;
script.metadata.grant = ["GM_getValue", "GM_getTab", "GM_saveTab", "GM_cookie"];
script.metadata.grant = ["GM_getValue", "GM_getTab", "GM_getTabs", "GM_saveTab", "GM_cookie"];
// @ts-ignore
const exec = new ExecScript(script, undefined, undefined, nilFn, envInfo);
script.code = `return {
["GM.getValue"]: GM.getValue,
["GM.getTab"]: GM.getTab,
["GM.setTab"]: GM.setTab,
["GM.getTabs"]: GM.getTabs,
["GM.saveTab"]: GM.saveTab,
GM_getValue: this.GM_getValue,
GM_getTab: this.GM_getTab,
GM_getTabs: this.GM_getTabs,
GM_saveTab: this.GM_saveTab,
GM_cookie: this.GM_cookie,
["GM_cookie.list"]: this.GM_cookie.list,
["GM.cookie"]: this.GM.cookie,
}`;
exec.scriptFunc = compileScript(compileScriptCode(script));
const ret = await exec.exec();
expect(ret["GM.getValue"]).toBeUndefined();
expect(ret["GM.getTab"]).toBeUndefined();
expect(ret["GM.setTab"]).toBeUndefined();
expect(ret.GM_getValue.name).toEqual("bound GM_getValue");
expect(ret.GM_getTab.name).toEqual("bound GM_getTab");
expect(ret.GM_saveTab.name).toEqual("bound GM_saveTab");
expect(ret.GM_cookie.name).toEqual("bound GM_cookie");
expect(ret["GM_cookie.list"].name).toEqual("bound GM_cookie.list");
expect(ret["GM.cookie"]).toBeUndefined();
// getValue
expect(ret.GM_getValue?.name).toEqual("bound GM_getValue");
expect(ret["GM.getValue"]?.name).toBeUndefined();
// getTab / getTabs / saveTab
expect(ret.GM_getTab?.name).toEqual("bound GM_getTab");
expect(ret.GM_getTabs?.name).toEqual("bound GM_getTabs");
expect(ret.GM_saveTab?.name).toEqual("bound GM_saveTab");
expect(ret["GM.getTab"]?.name).toBeUndefined();
expect(ret["GM.getTabs"]?.name).toBeUndefined();
expect(ret["GM.saveTab"]?.name).toBeUndefined();
// cookie
expect(ret.GM_cookie?.name).toEqual("bound GM_cookie");
expect(ret["GM_cookie.list"]?.name).toEqual("bound GM_cookie.list");
expect(ret["GM.cookie"]?.name).toBeUndefined();
});

it.concurrent("GM.*", async () => {
Expand All @@ -79,17 +86,20 @@ describe.concurrent("@grant GM", () => {
}`;
exec.scriptFunc = compileScript(compileScriptCode(script));
const ret = await exec.exec();
expect(ret["GM.getValue"].name).toEqual("bound GM.getValue");
expect(ret["GM.getTab"].name).toEqual("bound GM.getTab");
expect(ret["GM.getTabs"].name).toEqual("bound GM.getTabs");
expect(ret["GM.saveTab"].name).toEqual("bound GM_saveTab");
expect(ret.GM_getValue).toBeUndefined();
expect(ret.GM_getTab.name).toEqual("bound GM_getTab");
expect(ret.GM_getTabs.name).toEqual("bound GM_getTabs");
expect(ret.GM_saveTab).toBeUndefined();
expect(ret.GM_cookie).toBeUndefined();
expect(ret["GM.cookie"].name).toEqual("bound GM.cookie");
expect(ret["GM.cookie"].list.name).toEqual("bound GM.cookie.list");
// getValue
expect(ret["GM.getValue"]?.name).toEqual("bound GM.getValue");
expect(ret.GM_getValue?.name).toBeUndefined();
// getTab / getTabs / saveTab
// expect(ret.GM_getTab?.name).toBeUndefined();
// expect(ret.GM_getTabs?.name).toBeUndefined();
// expect(ret.GM_saveTab?.name).toBeUndefined();
expect(ret["GM.getTab"]?.name).toEqual("bound GM.getTab");
expect(ret["GM.getTabs"]?.name).toEqual("bound GM.getTabs");
expect(ret["GM.saveTab"]?.name).toEqual("bound GM.saveTab");
// cookie
expect(ret.GM_cookie?.name).toBeUndefined();
expect(ret["GM.cookie"]?.name).toEqual("bound GM.cookie");
expect(ret["GM.cookie"]?.list?.name).toEqual("bound GM.cookie.list");
});
});

Expand Down
Loading
Loading