Browse Source

fix #1983: show menu command for frame-only scripts

tophf 1 year ago
parent
commit
979adcd78d
4 changed files with 10 additions and 11 deletions
  1. 4 8
      src/injected/content/gm-api-content.js
  2. 2 1
      src/popup/index.js
  3. 1 1
      src/popup/utils/index.js
  4. 3 1
      src/popup/views/app.vue

+ 4 - 8
src/injected/content/gm-api-content.js

@@ -43,17 +43,13 @@ addHandlers({
   },
 
   RegisterMenu({ id, key, val }) {
-    if (window === top) {
-      (menus[id] || (menus[id] = createNullObj()))[key] = val;
-      sendSetPopup(true);
-    }
+    (menus[id] || (menus[id] = createNullObj()))[key] = val;
+    sendSetPopup(true);
   },
 
   UnregisterMenu({ id, key }) {
-    if (window === top) {
-      delete menus[id]?.[key];
-      sendSetPopup(true);
-    }
+    delete menus[id]?.[key];
+    sendSetPopup(true);
   },
 });
 

+ 2 - 1
src/popup/index.js

@@ -27,9 +27,10 @@ Object.assign(handlers, {
     const isTop = frameId === 0;
     if (!isTop) await mutex;
     else {
-      store.commands = data.menus;
       store[IS_APPLIED] = data[INJECT_INTO] !== 'off'; // isApplied at the time of GetInjected
     }
+    // Ensuring top script's menu wins over a per-frame menu with different commands
+    store.commands = Object.assign(data.menus, store.commands);
     const idMapAllFrames = store.idMap;
     const idMapMain = idMapAllFrames[0] || (idMapAllFrames[0] = {});
     const idMapOld = idMapAllFrames[frameId] || (idMapAllFrames[frameId] = {});

+ 1 - 1
src/popup/utils/index.js

@@ -4,7 +4,7 @@ export const emptyStore = () => ({
   scripts: [],
   frameScripts: [],
   idMap: {},
-  commands: [],
+  commands: {},
   domain: '',
   injectionFailure: null,
   injectable: true,

+ 3 - 1
src/popup/views/app.vue

@@ -425,12 +425,14 @@ export default {
         evt.preventDefault();
       } else if (type === 'keydown' || mousedownElement === el) {
         const [id, key, autoClose] = el.cmd;
+        const idMap = store.idMap;
+        const frameId = +Object.keys(idMap).find(frameIdStr => id in idMap[frameIdStr]);
         sendTabCmd(store.tab.id, 'Command', {
           id,
           key,
           evt: objectPick(evt, ['type', 'button', 'shiftKey', 'altKey', 'ctrlKey', 'metaKey',
             'key', 'keyCode', 'code']),
-        }).then(autoClose && close);
+        }, { [kFrameId]: frameId }).then(autoClose && close);
       }
     },
     onToggleScript(item) {