You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Chrome扩展ContextMenu监听器运行数分钟后失效问题求助

问题原因
  • Manifest V3的后台采用Service Worker实现,本身没有持久运行机制,空闲数分钟后会被系统自动回收终止,后续触发相关事件时才会重新冷启动。你的chrome.contextMenus.onClicked监听器注册逻辑放在了prepareContextMenuBySetting函数内部,该函数仅在扩展初始化、收到选项页更新消息时才会执行,Service Worker冷启动后不会主动执行该函数,导致监听器未注册,点击菜单无响应。
  • 你每次调用prepareContextMenuBySetting刷新菜单时都会重复添加同一个监听器,会导致正常运行时同一次点击触发多次回调的潜在问题。
  • 右键菜单本身存储在浏览器进程中,不会随Service Worker回收消失,但点击事件需要Service Worker中有已注册的监听器才能响应,打开控制台会延长Service Worker存活时间,所以功能失效时间会推迟,和你描述的现象完全匹配。
修复方案
  1. 将chrome.contextMenus.onClicked监听器移到background脚本的顶级同步作用域注册,确保Service Worker冷启动后第一时间就完成监听器注册,不需要依赖任何函数调用或者异步逻辑。
  2. 保留菜单动态生成逻辑,菜单ID和业务逻辑绑定即可,不需要每次生成菜单都重新注册监听器。
  3. 补充扩展安装/更新、浏览器启动时的初始化菜单逻辑,确保菜单始终和存储的配置一致。
  4. 修正异步逻辑顺序:chrome.contextMenus.removeAll是异步接口,需要在它的回调中执行菜单创建逻辑,避免偶发菜单被误删的问题。

修复后的完整代码如下:

// 顶级同步注册右键点击监听器,Service Worker冷启动必然执行
chrome.contextMenus.onClicked.addListener((info, tab) => {
  const rateKey = info.menuItemId.split("to");
  makeExchange(rateKey, info);
});

// 顶级同步注册消息监听器
chrome.runtime.onMessage.addListener(eventListener);

function eventListener(message, sender, sendResponse) {
  if (message.event === "updateMenu") {
    updateMenu();
    sendResponse({ result: "update menu completed" });
  } 
}

function updateMenu() {
  chrome.storage.local.get("currencyMappings", function (result) {
    prepareContextMenuBySetting(result.currencyMappings);
  });
}

function prepareContextMenuBySetting(mappings) {
  // 清空旧菜单完成后再创建新菜单
  chrome.contextMenus.removeAll(() => {
    chrome.contextMenus.create(
      { id: "rootMenu", title: "%s", contexts: ["selection"] },
      function () {
        if (chrome.extension.lastError) {
          console.log("Got expected error: " + chrome.extension.lastError.message);
        }
      }
    );
    mappings.forEach(map => {
      const currencies = map.split("|");
      chrome.contextMenus.create({
        id: `${currencies[0]}to${currencies[1]}`, 
        title: `${currencies[0]} => ${currencies[1]}`, 
        type: "normal", 
        parentId: "rootMenu", 
        contexts: ["selection"]
      });
    });
  });
}

// 补充扩展安装、浏览器启动时的菜单初始化逻辑
chrome.runtime.onInstalled.addListener(updateMenu);
chrome.runtime.onStartup.addListener(updateMenu);
额外说明

调整后无论Service Worker什么时候被回收重启,都会第一时间完成所有事件监听器的注册,不会再出现点击菜单无响应的问题,同时也修复了原代码中重复注册监听器、异步逻辑顺序错误的潜在问题。

内容的提问来源于stack exchange,提问作者Tzu Hao Chung

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.05 21:51:01