From a2da599980149ac4e44d480a26f9bc80c8666020 Mon Sep 17 00:00:00 2001 From: Aram Panasenco Date: Sat, 18 Mar 2023 10:20:33 -0600 Subject: [PATCH] Make background script of context-menu-copy-link-with-types non-persistent? (#501) * Made background script of context-menu-copy-link-with-types non-persistent. Now clearing errors that could result from background.js context menu creation happening more than once. * Suggestions from code review * Syntax error and formatting * Added note about () => void browser.runtime.lastError * Feedback suggestion Co-authored-by: Rob Wu --------- Co-authored-by: rebloor Co-authored-by: Rob Wu --- context-menu-copy-link-with-types/background.js | 16 ++++++++++++---- context-menu-copy-link-with-types/manifest.json | 3 ++- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/context-menu-copy-link-with-types/background.js b/context-menu-copy-link-with-types/background.js index 9152745b..1f965b7c 100644 --- a/context-menu-copy-link-with-types/background.js +++ b/context-menu-copy-link-with-types/background.js @@ -1,8 +1,16 @@ +// Callback reads runtime.lastError to prevent an unchecked error from being +// logged when the extension attempt to register the already-registered menu +// again. Menu registrations in event pages persist across extension restarts. browser.contextMenus.create({ - id: "copy-link-to-clipboard", - title: "Copy link to clipboard", - contexts: ["link"], -}); + id: "copy-link-to-clipboard", + title: "Copy link to clipboard", + contexts: ["link"], + }, + // See https://extensionworkshop.com/documentation/develop/manifest-v3-migration-guide/#event-pages-and-backward-compatibility + // for information on the purpose of this error capture. + () => void browser.runtime.lastError, +); + browser.contextMenus.onClicked.addListener((info, tab) => { if (info.menuItemId === "copy-link-to-clipboard") { // Examples: text and HTML to be copied. diff --git a/context-menu-copy-link-with-types/manifest.json b/context-menu-copy-link-with-types/manifest.json index b01b5ba7..eed02e54 100644 --- a/context-menu-copy-link-with-types/manifest.json +++ b/context-menu-copy-link-with-types/manifest.json @@ -8,7 +8,8 @@ "background": { "scripts": [ "background.js" - ] + ], + "persistent": false }, "permissions": [