Skip to content

Commit

Permalink
Hack: Periodically re-create context menus
Browse files Browse the repository at this point in the history
In Firefox, when context menus were created in MV3, they may disappear
unintentionally, even after restart. This is due to a bug in Firefox [1].

This patch introduces a hack which removes and re-creates all the
menus every 30 seconds.

[1]: https://discourse.mozilla.org/t/strange-mv3-behaviour-browser-runtime-oninstalled-event-and-menus-create/111208/7
  • Loading branch information
yorkxin committed Apr 16, 2024
1 parent f4828d5 commit a16292c
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
1 change: 1 addition & 0 deletions firefox/hacks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
globalThis["PERIDOCIALLY_REFRESH_MENU"] = true;

Check failure on line 1 in firefox/hacks.js

View workflow job for this annotation

GitHub Actions / build

'globalThis' is not defined

Check failure on line 1 in firefox/hacks.js

View workflow job for this annotation

GitHub Actions / build

["PERIDOCIALLY_REFRESH_MENU"] is better written in dot notation

Check failure on line 1 in firefox/hacks.js

View workflow job for this annotation

GitHub Actions / build

Strings must use singlequote
7 changes: 5 additions & 2 deletions firefox/manifest.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "Copy as Markdown",
"version": "2.8.3",
"version": "2.9.0",
"manifest_version": 3,
"description": "Copy Link or Image as Markdown code",
"permissions": [
Expand All @@ -26,7 +26,10 @@
"128": "./dist/images/icon-128.png"
},
"background": {
"scripts": ["./dist/background.js"],
"scripts": [
"hacks.js",
"./dist/background.js"
],
"type": "module"
},
"commands": {
Expand Down
19 changes: 17 additions & 2 deletions src/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ const TEXT_EMPTY = '';

const FLASH_BADGE_TIMEOUT = 3000; // ms

const ALARM_REFRESH_MENU = 'refreshMenu';

async function flashBadge(type) {
const entrypoint = chrome.action /* MV3 */ || chrome.browserAction; /* Firefox MV2 */

Expand Down Expand Up @@ -42,6 +44,10 @@ chrome.alarms.onAlarm.addListener((alarm) => {
])
.then(() => { /* NOP */ });
}

if (alarm.name === ALARM_REFRESH_MENU) {
chrome.contextMenus.removeAll(createMenus);

Check failure on line 49 in src/background.js

View workflow job for this annotation

GitHub Actions / build

'createMenus' was used before it was defined
}
});

async function handleContentOfContextMenu(info, tab) {
Expand Down Expand Up @@ -158,7 +164,7 @@ async function mustGetCurrentTab() {
return Promise.resolve(tabs[0]);
}

chrome.runtime.onInstalled.addListener(() => {
function createMenus() {
chrome.contextMenus.create({
id: 'current-page',
title: 'Copy [Page Title](URL)',
Expand All @@ -179,7 +185,16 @@ chrome.runtime.onInstalled.addListener(() => {
type: 'normal',
contexts: ['image'],
});
});
}

chrome.runtime.onInstalled.addListener(createMenus);

if (globalThis["PERIDOCIALLY_REFRESH_MENU"] === true) {

Check failure on line 192 in src/background.js

View workflow job for this annotation

GitHub Actions / build

'globalThis' is not defined

Check failure on line 192 in src/background.js

View workflow job for this annotation

GitHub Actions / build

["PERIDOCIALLY_REFRESH_MENU"] is better written in dot notation

Check failure on line 192 in src/background.js

View workflow job for this annotation

GitHub Actions / build

Strings must use singlequote
// Hack for Firefox, in which Context Menu disappears after some time.
// See https://discourse.mozilla.org/t/strange-mv3-behaviour-browser-runtime-oninstalled-event-and-menus-create/111208/7
console.info("Hack PERIDOCIALLY_REFRESH_MENU is enabled");

Check failure on line 195 in src/background.js

View workflow job for this annotation

GitHub Actions / build

Strings must use singlequote
chrome.alarms.create("refreshMenu", { periodInMinutes: 0.5});

Check failure on line 196 in src/background.js

View workflow job for this annotation

GitHub Actions / build

Strings must use singlequote

Check failure on line 196 in src/background.js

View workflow job for this annotation

GitHub Actions / build

A space is required before '}'
}

// NOTE: All listeners must be registered at top level scope.

Expand Down

0 comments on commit a16292c

Please sign in to comment.