Skip to content

Commit 9f6bffc

Browse files
committed
🐛 修复GM API加载问题
1 parent f49e20f commit 9f6bffc

File tree

5 files changed

+492
-485
lines changed

5 files changed

+492
-485
lines changed

example/grant_none.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// ==UserScript==
2+
// @name Grant None
3+
// @namespace https://bbs.tampermonkey.net.cn/
4+
// @version 0.1.0
5+
// @description try to take over the world!
6+
// @author You
7+
// @match https://bbs.tampermonkey.net.cn/
8+
// ==/UserScript==
9+
10+
console.log("Grant None", this, GM_info);
11+

src/app/service/content/create_context.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import type { ApiValue } from "./types";
44
import type { Message } from "@Packages/message/types";
55
import EventEmitter from "eventemitter3";
66
import { GMContextApiGet } from "./gm_context";
7-
import { GM_Base } from "./gm_base";
7+
import { GM_Base } from "./gm_api";
88

99
// 设置api依赖
1010
function setDepend(context: { [key: string]: any }, apiVal: ApiValue) {
@@ -109,4 +109,4 @@ export function createContext(scriptRes: ScriptRunResource, GMInfo: any, envPref
109109
}
110110
context.unsafeWindow = window;
111111
return <GM_Base>context;
112-
}
112+
}

src/app/service/content/gm_api.ts

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,65 @@ import { base64ToBlob } from "@App/pkg/utils/script";
55
import LoggerCore from "@App/app/logger/core";
66
import EventEmitter from "eventemitter3";
77
import GMContext from "./gm_context";
8-
import { GM_Base } from "./gm_base";
98
import { type ScriptRunResource } from "@App/app/repo/scripts";
9+
import type { ValueUpdateData } from "./types";
10+
import type { MessageRequest } from "../service_worker/types";
11+
import { connect, sendMessage } from "@Packages/message/client";
12+
import { getStorageName } from "@App/pkg/utils/utils";
13+
14+
export class GM_Base {
15+
runFlag!: string;
16+
17+
constructor(
18+
public prefix: string,
19+
public message: Message,
20+
public scriptRes: ScriptRunResource,
21+
public valueChangeListener: Map<number, { name: string; listener: GMTypes.ValueChangeListener }>,
22+
public EE: EventEmitter
23+
) {}
24+
25+
// 单次回调使用
26+
public sendMessage(api: string, params: any[]) {
27+
return sendMessage(this.message, this.prefix + "/runtime/gmApi", {
28+
uuid: this.scriptRes.uuid,
29+
api,
30+
params,
31+
runFlag: this.runFlag,
32+
} as MessageRequest);
33+
}
34+
35+
// 长连接使用,connect只用于接受消息,不发送消息
36+
public connect(api: string, params: any[]) {
37+
return connect(this.message, this.prefix + "/runtime/gmApi", {
38+
uuid: this.scriptRes.uuid,
39+
api,
40+
params,
41+
runFlag: this.runFlag,
42+
} as MessageRequest);
43+
}
44+
45+
public valueUpdate(data: ValueUpdateData) {
46+
if (data.uuid === this.scriptRes.uuid || data.storageName === getStorageName(this.scriptRes)) {
47+
// 触发,并更新值
48+
if (data.value === undefined) {
49+
if (this.scriptRes.value[data.key] !== undefined) {
50+
delete this.scriptRes.value[data.key];
51+
}
52+
} else {
53+
this.scriptRes.value[data.key] = data.value;
54+
}
55+
this.valueChangeListener.forEach((item) => {
56+
if (item.name === data.key) {
57+
item.listener(data.key, data.oldValue, data.value, data.sender.runFlag !== this.runFlag, data.sender.tabId);
58+
}
59+
});
60+
}
61+
}
62+
63+
emitEvent(event: string, eventId: string, data: any) {
64+
this.EE.emit(event + ":" + eventId, data);
65+
}
66+
}
1067

1168
export default class GMApi extends GM_Base {
1269
/**

src/app/service/content/gm_base.ts

Lines changed: 0 additions & 66 deletions
This file was deleted.

0 commit comments

Comments
 (0)