Skip to content

Fix "Just Save" context menu entry non appearing in Firefox >= 106 (where event pages support is enabled by default) #10

@rpl

Description

@rpl

This issue has been reported on bugzilla and so we have investigated it and described the underlying issue in more details in this comment on bugzilla:

In short the extension is opting-in on running the background script as an event page, but the "just-save.js" script is currently assuming that the background script will be persistent in Firefox and so the call to browser.contextMenus.create throws in Firefox >= 106 because the onclick property cannot be used when the script is running as an event page (in Firefox like in Chrome).

One quick way to fix the issue is to detect if the script is running as an event page in Firefox by leveraging the fact that browser.contextMenus.create will be throwing synchronously on the onclick property and based on that set isChrome to true, basically something like the following:

diff --git a/extension/just-save.js b/extension/just-save.js
index 152c303..68b245e 100644
--- a/extension/just-save.js
+++ b/extension/just-save.js
@@ -2,9 +2,26 @@
 
 let isChrome = window.browser === undefined;
 
+// Detect if running as an event page in Firefox.
+function detectFirefoxEventPageMode() {
+  if (isChrome) { return; }
+  try {
+    browser.contextMenus.create({ id: "test-menu", onclick: () => {} });
+  } catch (err) {
+    if (err?.message.includes("Property \"onclick\" cannot be used in menus.create")) {
+      // Set isChrome to true if Firefox is detected to be running with event pages support enabled.
+      isChrome = true;
+    }
+  } finally {
+    browser.contextMenus.remove("test-menu");
+  }
+}
+
 // adding browser shim for chrome support
 if (isChrome) {
   window.browser = chrome;
+} else {
+  detectFirefoxEventPageMode();
 }
 
 const logResult = result => {

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions