Skip to content

Commit 70c59e6

Browse files
committed
✨ 兼容GM_registerMenuCommand #358
1 parent 2240a62 commit 70c59e6

7 files changed

Lines changed: 77 additions & 24 deletions

File tree

example/gm_menu.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
// @description 创建菜单, 可以显示在右上角的插件弹出页和浏览器右键菜单中
66
// @author You
77
// @match https://bbs.tampermonkey.net.cn/
8-
// @grant GM_registerMenuCommand
9-
// @grant GM_unregisterMenuCommand
8+
// @grant GM_registerMenuCommand
9+
// @grant GM_unregisterMenuCommand
1010
// ==/UserScript==
1111

1212
const id = GM_registerMenuCommand(
@@ -15,7 +15,7 @@ const id = GM_registerMenuCommand(
1515
console.log(id);
1616
GM_unregisterMenuCommand(id);
1717
},
18-
"h"
18+
{ accessKey: "k", title: "测试菜单标题", autoClose: false }
1919
);
2020

2121
const id2 = GM_registerMenuCommand(
@@ -26,3 +26,15 @@ const id2 = GM_registerMenuCommand(
2626
},
2727
"j"
2828
);
29+
30+
setTimeout(() => {
31+
// 修改名字
32+
GM_registerMenuCommand(
33+
"修改后的测试菜单",
34+
() => {
35+
console.log("修改后的", id);
36+
GM_unregisterMenuCommand(id);
37+
},
38+
{ id: id, accessKey: "k", title: "修改后的测试菜单标题", autoClose: false }
39+
);
40+
}, 5000);

src/app/service/content/gm_api.ts

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -243,18 +243,29 @@ export default class GMApi {
243243
}
244244

245245
@GMContext.API()
246-
GM_registerMenuCommand(name: string, listener: () => void, accessKey?: string): number {
246+
GM_registerMenuCommand(name: string, listener: () => void, accessKey?: string | { id?: number }): number {
247247
if (!this.menuMap) {
248248
this.menuMap = new Map();
249249
}
250-
let flag = 0;
251-
this.menuMap.forEach((val, menuId) => {
252-
if (val === name) {
253-
flag = menuId;
250+
if (typeof accessKey === "object") {
251+
// 如果是对象,并且有id属性,则直接使用id
252+
if (accessKey.id && this.menuMap.has(accessKey.id)) {
253+
// 如果id存在,则直接使用
254+
this.EE.removeAllListeners("menuClick:" + accessKey.id);
255+
this.EE.addListener("menuClick:" + accessKey.id, listener);
256+
this.sendMessage("GM_registerMenuCommand", [accessKey.id, name, accessKey]);
257+
return accessKey.id;
258+
}
259+
} else {
260+
let flag = 0;
261+
this.menuMap.forEach((val, menuId) => {
262+
if (val === name) {
263+
flag = menuId;
264+
}
265+
});
266+
if (flag) {
267+
return flag;
254268
}
255-
});
256-
if (flag) {
257-
return flag;
258269
}
259270
this.eventId += 1;
260271
const id = this.eventId;

src/app/service/queue.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ export type ScriptMenuRegisterCallbackValue = {
4848
uuid: string;
4949
id: number;
5050
name: string;
51-
accessKey: string;
51+
options?: { autoClose?: string; title?: string; accessKey?: string };
5252
tabId: number;
5353
frameId: number;
5454
documentId: string;

src/app/service/service_worker/gm_api.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -645,7 +645,7 @@ export default class GMApi {
645645
uuid: request.script.uuid,
646646
id: id,
647647
name: name,
648-
accessKey: accessKey,
648+
options: typeof accessKey === "object" ? accessKey : { accessKey: accessKey },
649649
tabId: sender.getSender().tab?.id || -1,
650650
frameId: sender.getSender().frameId,
651651
documentId: sender.getSender().documentId,

src/app/service/service_worker/popup.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import { getStorageName } from "@App/pkg/utils/utils";
2525
export type ScriptMenuItem = {
2626
id: number;
2727
name: string;
28-
accessKey?: string;
28+
options?: { autoClose?: string; title?: string; accessKey?: string };
2929
tabId: number; //-1表示后台脚本
3030
frameId?: number;
3131
documentId?: string;
@@ -119,11 +119,15 @@ export class PopupService {
119119
script.menus.push({
120120
id: message.id,
121121
name: message.name,
122-
accessKey: message.accessKey,
122+
options: message.options,
123123
tabId: message.tabId,
124124
frameId: message.frameId,
125125
documentId: message.documentId,
126126
});
127+
} else {
128+
// 存在修改信息
129+
menu.name = message.name;
130+
menu.options = message.options;
127131
}
128132
}
129133
this.updateScriptMenu();

src/pages/components/ScriptMenuList/index.tsx

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,26 @@ const ScriptMenuList: React.FC<{
5555
}
5656
useEffect(() => {
5757
setList(script);
58+
// 注册菜单快捷键
59+
const listeners: ((e: KeyboardEvent) => void)[] = [];
60+
script.forEach((item) => {
61+
item.menus.forEach((menu) => {
62+
if (menu.options?.accessKey) {
63+
const listener = (e: KeyboardEvent) => {
64+
if (e.key.toUpperCase() === menu.options!.accessKey!.toUpperCase()) {
65+
sendMenuAction(item.uuid, menu);
66+
}
67+
};
68+
document.addEventListener("keypress", listener);
69+
listeners.push(listener);
70+
}
71+
});
72+
});
73+
return () => {
74+
listeners.forEach((listener) => {
75+
document.removeEventListener("keypress", listener);
76+
});
77+
};
5878
}, [script]);
5979

6080
useEffect(() => {
@@ -209,25 +229,20 @@ const ScriptMenuList: React.FC<{
209229
: item.menus?.slice(0, menuExpandNum)
210230
: item.menus
211231
)?.map((menu) => {
212-
if (menu.accessKey) {
213-
document.addEventListener("keypress", (e) => {
214-
if (e.key.toUpperCase() === menu.accessKey!.toUpperCase()) {
215-
sendMenuAction(item.uuid, menu);
216-
}
217-
});
218-
}
232+
console.log("menu", menu);
219233
return (
220234
<Button
221235
className="text-left"
222236
key={menu.id}
223237
type="secondary"
224238
icon={<IconMenu />}
239+
title={menu.options?.title}
225240
onClick={() => {
226241
sendMenuAction(item.uuid, menu);
227242
}}
228243
>
229244
{menu.name}
230-
{menu.accessKey && `(${menu.accessKey.toUpperCase()})`}
245+
{menu.options?.accessKey && `(${menu.options.accessKey.toUpperCase()})`}
231246
</Button>
232247
);
233248
})}

src/types/scriptcat.d.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,18 @@ declare function GM_getResourceText(name: string): string | undefined;
9797

9898
declare function GM_getResourceURL(name: string, isBlobUrl?: boolean): string | undefined;
9999

100-
declare function GM_registerMenuCommand(name: string, listener: () => void, accessKey?: string): number;
100+
declare function GM_registerMenuCommand(
101+
name: string,
102+
listener: () => void,
103+
optionsOrAccessKey?:
104+
| string
105+
| {
106+
id?: number | string;
107+
accessKey?: string;
108+
autoClose?: boolean;
109+
title?: string;
110+
}
111+
): number;
101112

102113
declare function GM_unregisterMenuCommand(id: number): void;
103114

0 commit comments

Comments
 (0)