Skip to content

Commit 9515269

Browse files
committed
🐛 修复菜单多次注册的问题 #129
1 parent 2d5573f commit 9515269

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

src/runtime/content/gm_api.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -746,12 +746,26 @@ export default class GMApi {
746746

747747
menuId: number | undefined;
748748

749+
menuMap: Map<number, string> | undefined;
750+
749751
@GMContext.API()
750752
GM_registerMenuCommand(
751753
name: string,
752754
listener: () => void,
753755
accessKey?: string
754756
): number {
757+
if (!this.menuMap) {
758+
this.menuMap = new Map();
759+
}
760+
let flag = 0;
761+
this.menuMap.forEach((val, key) => {
762+
if (val === name) {
763+
flag = key;
764+
}
765+
});
766+
if (flag) {
767+
return flag;
768+
}
755769
if (!this.menuId) {
756770
this.menuId = 1;
757771
} else {
@@ -761,11 +775,16 @@ export default class GMApi {
761775
this.connect("GM_registerMenuCommand", [id, name, accessKey], () => {
762776
listener();
763777
});
778+
this.menuMap.set(id, name);
764779
return id;
765780
}
766781

767782
@GMContext.API()
768783
GM_unregisterMenuCommand(id: number): void {
784+
if (!this.menuMap) {
785+
this.menuMap = new Map();
786+
}
787+
this.menuMap.delete(id);
769788
this.sendMessage("GM_unregisterMenuCommand", [id]);
770789
}
771790

0 commit comments

Comments
 (0)