-
Notifications
You must be signed in to change notification settings - Fork 310
[高优先] 重新修订 GM_registerMenuCommand 相关代码设计
#790
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
CodFrm
merged 30 commits into
scriptscat:main
from
cyfung1031:GM_registerMenuCommand_codefix
Oct 9, 2025
Merged
Changes from all commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
09e7392
重新修订 `GM_registerMenuCommand` 相关代码设计
cyfung1031 4c854d0
修正 Popup tsx 状态更新问题
cyfung1031 ad453bd
简化 grouppedMenus 代码
cyfung1031 e414b51
小修正
cyfung1031 3125f3c
Copilot说得对
cyfung1031 a04c139
Copilot说得对
cyfung1031 7e8981d
Copilot说得对
cyfung1031 b779c49
将TODO注释提取为统一的文档或issue。
cyfung1031 3b6f520
将TODO注释提取为统一的文档或issue。
cyfung1031 27d828d
lint
cyfung1031 f7750a1
整理注释成 VSCode IntelliSense 版
cyfung1031 f099b1e
Merge branch 'main' into GM_registerMenuCommand_codefix
cyfung1031 cc57167
merge修正
cyfung1031 0f7280f
[script, grouppedMenus]);
cyfung1031 8dbc5ec
Merge remote-tracking branch 'upstream/main' into GM_registerMenuComm…
cyfung1031 2344f7a
Merge remote-tracking branch 'upstream/main' into GM_registerMenuComm…
cyfung1031 4f676f8
防止热重启打乱 id
cyfung1031 2d869ba
不需要 contextMenuConvArr
cyfung1031 05582cd
處理一下 chrome.contextMenus.create 的 Runtime lastError
cyfung1031 d337b86
添加单元测试
CodFrm 59d74bf
Merge remote-tracking branch 'origin/main' into pr/cyfung1031/790
CodFrm 48824f0
减少 `JSON.stringify` 的呼叫
cyfung1031 4831c68
中文
cyfung1031 69c7456
增加单元测试
CodFrm 276e2a6
Update src/app/service/content/gm_api.ts
CodFrm 831503c
Update src/app/service/service_worker/popup.ts
CodFrm 79ac078
删除menuRegisterNeedUpdate 和 menuRegister逻辑
CodFrm dc14f0f
Merge branch 'GM_registerMenuCommand_codefix' of github.com:cyfung103…
CodFrm 6445c0a
修复并发问题
CodFrm b8b1e7d
修改为shift
CodFrm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,14 @@ | ||
| import type { Message, MessageConnect } from "@Packages/message/types"; | ||
| import type { CustomEventMessage } from "@Packages/message/custom_event_message"; | ||
| import type { NotificationMessageOption, ScriptMenuItem } from "../service_worker/types"; | ||
| import { base64ToBlob, strToBase64 } from "@App/pkg/utils/utils"; | ||
| import type { | ||
| GMRegisterMenuCommandParam, | ||
| GMUnRegisterMenuCommandParam, | ||
| NotificationMessageOption, | ||
| ScriptMenuItemOption, | ||
| TScriptMenuItemID, | ||
| TScriptMenuItemKey, | ||
| } from "../service_worker/types"; | ||
| import { base64ToBlob, randomMessageFlag, strToBase64 } from "@App/pkg/utils/utils"; | ||
| import LoggerCore from "@App/app/logger/core"; | ||
| import EventEmitter from "eventemitter3"; | ||
| import GMContext from "./gm_context"; | ||
|
|
@@ -21,6 +28,14 @@ export interface IGM_Base { | |
|
|
||
| const integrity = {}; // 仅防止非法实例化 | ||
|
|
||
| const execEnvInit = (execEnv: GMApi) => { | ||
| if (!execEnv.contentEnvKey) { | ||
| execEnv.contentEnvKey = randomMessageFlag(); // 不重复识别字串。用于区分 mainframe subframe 等执行环境 | ||
| execEnv.menuKeyRegistered = new Set(); | ||
| execEnv.menuIdCounter = 0; | ||
| } | ||
| }; | ||
|
|
||
| // GM_Base 定义内部用变量和函数。均使用@protected | ||
| // 暂不考虑 Object.getOwnPropertyNames(GM_Base.prototype) 和 ts-morph 脚本生成 | ||
| class GM_Base implements IGM_Base { | ||
|
|
@@ -440,52 +455,59 @@ export default class GMApi extends GM_Base { | |
| _GM_cookie(this, action, details, done); | ||
| } | ||
|
|
||
| menuMap: Map<number, string> | undefined; | ||
| // 已注册的「菜单唯一键」集合,用于去重与解除绑定。 | ||
| // 唯一键格式:{contentEnvKey}.t{注册ID},由 execEnvInit() 建立/维护。 | ||
| menuKeyRegistered: Set<string> | undefined; | ||
|
|
||
| // 自动产生的菜单 ID 累计器(仅在未提供 options.id 时使用)。 | ||
| // 每个 contentEnvKey(执行环境)初始化时会重设;不持久化、只保证当前环境内递增唯一。 | ||
| menuIdCounter: number | undefined; | ||
|
|
||
| // 内容脚本执行环境识别符,用于区分 mainframe / subframe 等环境并作为 menu key 的命名空间。 | ||
| // 由 execEnvInit() 以 randomMessageFlag() 生成,避免跨 frame 的 ID 碰撞。 | ||
| // (同一环境跨脚本也不一样) | ||
| contentEnvKey: string | undefined; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 可以使用 runFlag ,不过另外再生成一个也没问题,语义上不同,只是提一下有 runFlag 也可以用于区分不同的环境/脚本 |
||
|
|
||
| @GMContext.API({ alias: "GM.registerMenuCommand" }) | ||
| GM_registerMenuCommand( | ||
| name: string, | ||
| listener: (inputValue?: any) => void, | ||
| options_or_accessKey?: ScriptMenuItem["options"] | string | ||
| ): number { | ||
| if (!this.menuMap) { | ||
| this.menuMap = new Map(); | ||
| } | ||
| if (typeof options_or_accessKey === "object") { | ||
| const option: ScriptMenuItem["options"] = options_or_accessKey; | ||
| // 如果是对象,并且有id属性,则直接使用id | ||
| if (option.id && this.menuMap.has(option.id)) { | ||
| // 如果id存在,则直接使用 | ||
| this.EE.removeAllListeners("menuClick:" + option.id); | ||
| this.EE.addListener("menuClick:" + option.id, listener); | ||
| this.sendMessage("GM_registerMenuCommand", [option.id, name, option]); | ||
| return option.id; | ||
| } | ||
| options_or_accessKey?: ScriptMenuItemOption | string | ||
| ): TScriptMenuItemID { | ||
| execEnvInit(this); | ||
| // 浅拷贝避免修改/共用参数 | ||
| const options = ( | ||
| typeof options_or_accessKey === "string" | ||
| ? { accessKey: options_or_accessKey } | ||
| : options_or_accessKey | ||
| ? { ...options_or_accessKey } | ||
| : {} | ||
| ) as ScriptMenuItemOption; | ||
| let providedId: string | number | undefined = options.id; | ||
| delete options.id; // id不直接储存在options (id 影响 groupKey 操作) | ||
| if (providedId === undefined) providedId = this.menuIdCounter! += 1; // 如无指定,使用累计器id | ||
| const ret = providedId as TScriptMenuItemID; | ||
| providedId = `t${providedId}`; // 见 TScriptMenuItemID 注释 | ||
| providedId = `${this.contentEnvKey!}.${providedId}` as TScriptMenuItemKey; // 区分 subframe mainframe,见 TScriptMenuItemKey 注释 | ||
| const menuKey = providedId; // menuKey为唯一键:{环境识别符}.t{注册ID} | ||
| // 检查之前有否注册 | ||
| if (menuKey && this.menuKeyRegistered!.has(menuKey)) { | ||
| // 有注册过,先移除 listeners | ||
| this.EE.removeAllListeners("menuClick:" + menuKey); | ||
| } else { | ||
| options_or_accessKey = { accessKey: options_or_accessKey }; | ||
| let flag = 0; | ||
| this.menuMap.forEach((val, menuId) => { | ||
| if (val === name) { | ||
| flag = menuId; | ||
| } | ||
| }); | ||
| if (flag) { | ||
| return flag; | ||
| } | ||
| // 没注册过,先记录一下 | ||
| this.menuKeyRegistered!.add(menuKey); | ||
| } | ||
| this.eventId += 1; | ||
| const id = this.eventId; | ||
| options_or_accessKey.id = id; | ||
| this.menuMap.set(id, name); | ||
| this.EE.addListener("menuClick:" + id, listener); | ||
| this.sendMessage("GM_registerMenuCommand", [id, name, options_or_accessKey]); | ||
| return id; | ||
| this.EE.addListener("menuClick:" + menuKey, listener); | ||
| // 发送至 service worker 处理(唯一键,显示名字,不包括id的其他设定) | ||
| this.sendMessage("GM_registerMenuCommand", [menuKey, name, options] as GMRegisterMenuCommandParam); | ||
| return ret; | ||
| } | ||
|
|
||
| @GMContext.API({ | ||
| depend: ["GM_registerMenuCommand"], | ||
| }) | ||
| CAT_registerMenuInput(...args: Parameters<GMApi["GM_registerMenuCommand"]>): number { | ||
| CAT_registerMenuInput(...args: Parameters<GMApi["GM_registerMenuCommand"]>): TScriptMenuItemID { | ||
| return this.GM_registerMenuCommand(...args); | ||
| } | ||
|
|
||
|
|
@@ -543,13 +565,16 @@ export default class GMApi extends GM_Base { | |
| } | ||
|
|
||
| @GMContext.API({ alias: "GM.unregisterMenuCommand" }) | ||
| GM_unregisterMenuCommand(id: number): void { | ||
| if (!this.menuMap) { | ||
| this.menuMap = new Map(); | ||
| GM_unregisterMenuCommand(menuId: TScriptMenuItemID): void { | ||
| if (!this.contentEnvKey) { | ||
| return; | ||
| } | ||
| this.menuMap.delete(id); | ||
| this.EE.removeAllListeners("menuClick:" + id); | ||
| this.sendMessage("GM_unregisterMenuCommand", [id]); | ||
| let menuKey = `t${menuId}`; // 见 TScriptMenuItemID 注释 | ||
| menuKey = `${this.contentEnvKey!}.${menuKey}` as TScriptMenuItemKey; // 区分 subframe mainframe,见 TScriptMenuItemKey 注释 | ||
| this.menuKeyRegistered!.delete(menuKey); | ||
| this.EE.removeAllListeners("menuClick:" + menuKey); | ||
| // 发送至 service worker 处理(唯一键) | ||
| this.sendMessage("GM_unregisterMenuCommand", [menuKey] as GMUnRegisterMenuCommandParam); | ||
| } | ||
|
|
||
| @GMContext.API({ | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
啥意思?