Skip to content

Commit

Permalink
Workaround: Menus disappears on Firefox when restarted
Browse files Browse the repository at this point in the history
`chrome.runtime.onInstalled` is not called when Firefox restarted. This behavior is inconsistent with Chrome.

See https://bugzilla.mozilla.org/show_bug.cgi?id=1567467
  • Loading branch information
yorkxin committed Sep 4, 2022
1 parent fbc5e4a commit 001186a
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
1 change: 1 addition & 0 deletions firefox-mv2/background.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
<head><meta charset="utf-8"></head>
<body>
<script type="module" src="./dist/background.js"></script>
<script type="module" src="./create-menus.js"></script>
</body>
</html>
37 changes: 37 additions & 0 deletions firefox-mv2/create-menus.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// This file is a workaround for Firefox event page.
// It is executed every time background.js is loaded.
//
// In Chrome/Chromium, browsers, the suggested way to create a context menu is calling
// `chrome.contextMenu.create()` in `chrome.runtime.onInstalled()`. This function will be called
// when browser is restarted.
//
// In Firefox, the behavior is slightly different, such that onInstalled() is called ONLY at the
// first time the add-on is installed. It is NOT called when the browser is restarted.
//
// As a workaround, in Firefox, call contextMenus.create() everytime background.js is loaded.
//
// For more details, see the following issues on Firefox's bug tracker:
//
// * https://bugzilla.mozilla.org/show_bug.cgi?id=1567467
// * https://bugzilla.mozilla.org/show_bug.cgi?id=1558336

chrome.contextMenus.create({
id: 'current-page',
title: 'Copy [Page Title](URL)',
type: 'normal',
contexts: ['page'],
});

chrome.contextMenus.create({
id: 'link',
title: 'Copy [Link Content](URL)',
type: 'normal',
contexts: ['link'],
});

chrome.contextMenus.create({
id: 'image',
title: 'Copy ![](Image URL)', // TODO: how to fetch alt text?
type: 'normal',
contexts: ['image'],
});
2 changes: 1 addition & 1 deletion firefox-mv2/manifest.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "Copy as Markdown",
"version": "2.6.0",
"version": "2.6.2rc1",
"manifest_version": 2,
"description": "Copy Link or Image as Markdown code",
"permissions": [
Expand Down

0 comments on commit 001186a

Please sign in to comment.