Skip to content

Commit e1886e5

Browse files
committed
refactor: extract GM menu handler
1 parent 4f30047 commit e1886e5

13 files changed

Lines changed: 624 additions & 223 deletions

background.core.js

Lines changed: 7 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -8896,82 +8896,15 @@ async function handleMessage(message, sender) {
88968896
scriptMetaStr: null
88978897
};
88988898

8899-
// Register menu command (with extended options: id, accessKey, autoClose, title)
8899+
// GM menu commands and dashboard menu-command execution
89008900
case 'registerMenuCommand':
8901-
case 'GM_registerMenuCommand': {
8902-
const commands = await chrome.storage.session.get('menuCommands') || {};
8903-
if (!commands.menuCommands) commands.menuCommands = {};
8904-
if (!commands.menuCommands[data.scriptId]) commands.menuCommands[data.scriptId] = [];
8905-
8906-
// If command with same id exists, update it instead of adding duplicate
8907-
const existing = commands.menuCommands[data.scriptId].findIndex(c => c.id === data.commandId);
8908-
const cmdEntry = {
8909-
id: data.commandId,
8910-
caption: data.caption,
8911-
accessKey: data.accessKey || '',
8912-
autoClose: data.autoClose !== false,
8913-
title: data.title || ''
8914-
};
8915-
if (existing >= 0) {
8916-
commands.menuCommands[data.scriptId][existing] = cmdEntry;
8917-
} else {
8918-
commands.menuCommands[data.scriptId].push(cmdEntry);
8919-
}
8920-
8921-
await chrome.storage.session.set(commands);
8922-
return { success: true };
8923-
}
8924-
8901+
case 'GM_registerMenuCommand':
89258902
case 'unregisterMenuCommand':
8926-
case 'GM_unregisterMenuCommand': {
8927-
const commands = await chrome.storage.session.get('menuCommands') || {};
8928-
if (commands.menuCommands?.[data.scriptId]) {
8929-
commands.menuCommands[data.scriptId] = commands.menuCommands[data.scriptId].filter(
8930-
c => c.id !== data.commandId
8931-
);
8932-
if (commands.menuCommands[data.scriptId].length === 0) {
8933-
delete commands.menuCommands[data.scriptId];
8934-
}
8935-
await chrome.storage.session.set(commands);
8936-
}
8937-
return { success: true };
8938-
}
8939-
8940-
// Get menu commands
8941-
case 'getMenuCommands': {
8942-
const result = await chrome.storage.session.get('menuCommands');
8943-
const allCommands = result?.menuCommands || {};
8944-
const commands = [];
8945-
8946-
// Flatten commands and add script info
8947-
const scripts = await ScriptStorage.getAll();
8948-
for (const [scriptId, cmds] of Object.entries(allCommands)) {
8949-
const script = scripts.find(s => s.id === scriptId);
8950-
if (script && cmds) {
8951-
cmds.forEach(cmd => {
8952-
commands.push({
8953-
...cmd,
8954-
scriptId,
8955-
scriptName: script.meta?.name || 'Unknown Script'
8956-
});
8957-
});
8958-
}
8959-
}
8960-
8961-
return { commands };
8962-
}
8963-
8964-
// Execute menu command
8965-
case 'executeMenuCommand': {
8966-
// Send to content script
8967-
if (sender.tab?.id) {
8968-
await chrome.tabs.sendMessage(sender.tab.id, {
8969-
action: 'executeMenuCommand',
8970-
data: { scriptId: data.scriptId, commandId: data.commandId }
8971-
});
8972-
}
8973-
return { success: true };
8974-
}
8903+
case 'GM_unregisterMenuCommand':
8904+
case 'getMenuCommands':
8905+
case 'executeMenuCommand':
8906+
if (typeof GMMenuHandler === 'undefined') return { error: 'GMMenuHandler not available' };
8907+
return await GMMenuHandler.handleGMMenuMessage(action, data, sender);
89758908

89768909
// GM_cookie API
89778910
// chrome.cookies.* only accepts http(s) URLs. Front-validate so blob:/

background.js

Lines changed: 142 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -18051,6 +18051,141 @@ if (typeof self !== 'undefined') {
1805118051
self.GMAudioHandler = GMAudioHandler;
1805218052
}
1805318053

18054+
// ============================================================================
18055+
// Generated from src/background/gm-menu-handler.ts; do not edit by hand.
18056+
// Run `node scripts/generate-ts-runtime-modules.mjs` or `npm run build:bg`.
18057+
// ============================================================================
18058+
18059+
const GMMenuHandler = (() => {
18060+
const module = { exports: {} };
18061+
const exports = module.exports;
18062+
"use strict";
18063+
var __defProp = Object.defineProperty;
18064+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
18065+
var __getOwnPropNames = Object.getOwnPropertyNames;
18066+
var __hasOwnProp = Object.prototype.hasOwnProperty;
18067+
var __export = (target, all) => {
18068+
for (var name in all)
18069+
__defProp(target, name, { get: all[name], enumerable: true });
18070+
};
18071+
var __copyProps = (to, from, except, desc) => {
18072+
if (from && typeof from === "object" || typeof from === "function") {
18073+
for (let key of __getOwnPropNames(from))
18074+
if (!__hasOwnProp.call(to, key) && key !== except)
18075+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
18076+
}
18077+
return to;
18078+
};
18079+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18080+
18081+
// src/background/gm-menu-handler.ts
18082+
var gm_menu_handler_exports = {};
18083+
__export(gm_menu_handler_exports, {
18084+
GMMenuHandler: () => GMMenuHandler,
18085+
GM_MENU_ACTIONS: () => GM_MENU_ACTIONS,
18086+
default: () => gm_menu_handler_default,
18087+
handleGMMenuMessage: () => handleGMMenuMessage,
18088+
isGMMenuAction: () => isGMMenuAction
18089+
});
18090+
module.exports = __toCommonJS(gm_menu_handler_exports);
18091+
var GM_MENU_ACTIONS = [
18092+
"executeMenuCommand",
18093+
"getMenuCommands",
18094+
"GM_registerMenuCommand",
18095+
"GM_unregisterMenuCommand",
18096+
"registerMenuCommand",
18097+
"unregisterMenuCommand"
18098+
];
18099+
var GM_MENU_ACTION_SET = new Set(GM_MENU_ACTIONS);
18100+
function isGMMenuAction(action) {
18101+
return typeof action === "string" && GM_MENU_ACTION_SET.has(action);
18102+
}
18103+
async function handleGMMenuMessage(action, data = {}, sender = {}) {
18104+
switch (action) {
18105+
case "registerMenuCommand":
18106+
case "GM_registerMenuCommand": {
18107+
const scriptId = data.scriptId;
18108+
const commands = await chrome.storage.session.get("menuCommands") || {};
18109+
if (!commands.menuCommands) commands.menuCommands = {};
18110+
if (!commands.menuCommands[scriptId]) commands.menuCommands[scriptId] = [];
18111+
const existing = commands.menuCommands[scriptId].findIndex(
18112+
(command) => command.id === data.commandId
18113+
);
18114+
const cmdEntry = {
18115+
id: data.commandId,
18116+
caption: data.caption,
18117+
accessKey: data.accessKey || "",
18118+
autoClose: data.autoClose !== false,
18119+
title: data.title || ""
18120+
};
18121+
if (existing >= 0) {
18122+
commands.menuCommands[scriptId][existing] = cmdEntry;
18123+
} else {
18124+
commands.menuCommands[scriptId].push(cmdEntry);
18125+
}
18126+
await chrome.storage.session.set(commands);
18127+
return { success: true };
18128+
}
18129+
case "unregisterMenuCommand":
18130+
case "GM_unregisterMenuCommand": {
18131+
const scriptId = data.scriptId;
18132+
const commands = await chrome.storage.session.get("menuCommands") || {};
18133+
if (commands.menuCommands?.[scriptId]) {
18134+
commands.menuCommands[scriptId] = commands.menuCommands[scriptId].filter(
18135+
(command) => command.id !== data.commandId
18136+
);
18137+
if (commands.menuCommands[scriptId].length === 0) {
18138+
delete commands.menuCommands[scriptId];
18139+
}
18140+
await chrome.storage.session.set(commands);
18141+
}
18142+
return { success: true };
18143+
}
18144+
case "getMenuCommands": {
18145+
const result = await chrome.storage.session.get("menuCommands");
18146+
const allCommands = result?.menuCommands || {};
18147+
const commands = [];
18148+
const scripts = await ScriptStorage.getAll();
18149+
for (const [scriptId, menuCommands] of Object.entries(allCommands)) {
18150+
const script = scripts.find((candidate) => candidate.id === scriptId);
18151+
if (script && menuCommands) {
18152+
menuCommands.forEach((command) => {
18153+
commands.push({
18154+
...command,
18155+
scriptId,
18156+
scriptName: script.meta?.name || "Unknown Script"
18157+
});
18158+
});
18159+
}
18160+
}
18161+
return { commands };
18162+
}
18163+
case "executeMenuCommand": {
18164+
if (sender.tab?.id) {
18165+
await chrome.tabs.sendMessage(sender.tab.id, {
18166+
action: "executeMenuCommand",
18167+
data: { scriptId: data.scriptId, commandId: data.commandId }
18168+
});
18169+
}
18170+
return { success: true };
18171+
}
18172+
default:
18173+
return { error: `Unsupported menu command action: ${action}` };
18174+
}
18175+
}
18176+
var GMMenuHandler = Object.freeze({
18177+
GM_MENU_ACTIONS,
18178+
handleGMMenuMessage,
18179+
isGMMenuAction
18180+
});
18181+
var gm_menu_handler_default = GMMenuHandler;
18182+
return module.exports.default || module.exports.GMMenuHandler || module.exports;
18183+
})();
18184+
18185+
if (typeof self !== 'undefined') {
18186+
self.GMMenuHandler = GMMenuHandler;
18187+
}
18188+
1805418189
// ============================================================================
1805518190
// Generated from src/background/connect-policy.ts; do not edit by hand.
1805618191
// Run `node scripts/generate-ts-runtime-modules.mjs` or `npm run build:bg`.
@@ -37738,82 +37873,15 @@ async function handleMessage(message, sender) {
3773837873
scriptMetaStr: null
3773937874
};
3774037875

37741-
// Register menu command (with extended options: id, accessKey, autoClose, title)
37876+
// GM menu commands and dashboard menu-command execution
3774237877
case 'registerMenuCommand':
37743-
case 'GM_registerMenuCommand': {
37744-
const commands = await chrome.storage.session.get('menuCommands') || {};
37745-
if (!commands.menuCommands) commands.menuCommands = {};
37746-
if (!commands.menuCommands[data.scriptId]) commands.menuCommands[data.scriptId] = [];
37747-
37748-
// If command with same id exists, update it instead of adding duplicate
37749-
const existing = commands.menuCommands[data.scriptId].findIndex(c => c.id === data.commandId);
37750-
const cmdEntry = {
37751-
id: data.commandId,
37752-
caption: data.caption,
37753-
accessKey: data.accessKey || '',
37754-
autoClose: data.autoClose !== false,
37755-
title: data.title || ''
37756-
};
37757-
if (existing >= 0) {
37758-
commands.menuCommands[data.scriptId][existing] = cmdEntry;
37759-
} else {
37760-
commands.menuCommands[data.scriptId].push(cmdEntry);
37761-
}
37762-
37763-
await chrome.storage.session.set(commands);
37764-
return { success: true };
37765-
}
37766-
37878+
case 'GM_registerMenuCommand':
3776737879
case 'unregisterMenuCommand':
37768-
case 'GM_unregisterMenuCommand': {
37769-
const commands = await chrome.storage.session.get('menuCommands') || {};
37770-
if (commands.menuCommands?.[data.scriptId]) {
37771-
commands.menuCommands[data.scriptId] = commands.menuCommands[data.scriptId].filter(
37772-
c => c.id !== data.commandId
37773-
);
37774-
if (commands.menuCommands[data.scriptId].length === 0) {
37775-
delete commands.menuCommands[data.scriptId];
37776-
}
37777-
await chrome.storage.session.set(commands);
37778-
}
37779-
return { success: true };
37780-
}
37781-
37782-
// Get menu commands
37783-
case 'getMenuCommands': {
37784-
const result = await chrome.storage.session.get('menuCommands');
37785-
const allCommands = result?.menuCommands || {};
37786-
const commands = [];
37787-
37788-
// Flatten commands and add script info
37789-
const scripts = await ScriptStorage.getAll();
37790-
for (const [scriptId, cmds] of Object.entries(allCommands)) {
37791-
const script = scripts.find(s => s.id === scriptId);
37792-
if (script && cmds) {
37793-
cmds.forEach(cmd => {
37794-
commands.push({
37795-
...cmd,
37796-
scriptId,
37797-
scriptName: script.meta?.name || 'Unknown Script'
37798-
});
37799-
});
37800-
}
37801-
}
37802-
37803-
return { commands };
37804-
}
37805-
37806-
// Execute menu command
37807-
case 'executeMenuCommand': {
37808-
// Send to content script
37809-
if (sender.tab?.id) {
37810-
await chrome.tabs.sendMessage(sender.tab.id, {
37811-
action: 'executeMenuCommand',
37812-
data: { scriptId: data.scriptId, commandId: data.commandId }
37813-
});
37814-
}
37815-
return { success: true };
37816-
}
37880+
case 'GM_unregisterMenuCommand':
37881+
case 'getMenuCommands':
37882+
case 'executeMenuCommand':
37883+
if (typeof GMMenuHandler === 'undefined') return { error: 'GMMenuHandler not available' };
37884+
return await GMMenuHandler.handleGMMenuMessage(action, data, sender);
3781737885

3781837886
// GM_cookie API
3781937887
// chrome.cookies.* only accepts http(s) URLs. Front-validate so blob:/

esbuild.config.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ async function buildBackground() {
108108
readFile("modules/user-script-message-policy.js"),
109109
readFile("modules/message-router.js"),
110110
readFile("modules/gm-audio-handler.js"),
111+
readFile("modules/gm-menu-handler.js"),
111112
readFile("modules/connect-policy.js"),
112113
readFile("modules/resources.js"),
113114

0 commit comments

Comments
 (0)