From e74883e9c22b52256d20cd9e945105abae1549a8 Mon Sep 17 00:00:00 2001 From: Gabe Yuan Date: Mon, 5 Feb 2024 10:51:42 +0800 Subject: [PATCH] fix: contextMenus: duplicate id err --- src/background.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/background.js b/src/background.js index 241d5d3..5f8f848 100644 --- a/src/background.js +++ b/src/background.js @@ -28,7 +28,14 @@ globalThis.ContextType = "BACKGROUND"; /** * 添加右键菜单 */ -function addContextMenus() { +async function addContextMenus() { + // 添加前先删除,避免重复ID的错误 + try { + await browser.contextMenus.removeAll(); + } catch (err) { + // + } + browser.contextMenus.create({ id: CMD_TOGGLE_TRANSLATE, title: browser.i18n.getMessage("toggle_translate"), @@ -93,6 +100,7 @@ browser.runtime.onStartup.addListener(async () => { // 右键菜单 if (contextMenus) { + // firefox重启后菜单会消失,故重复添加 addContextMenus(); } else { removeContextMenus();