WebExtension Context Menu Item disappear after Firefox restart/close


(Pcloudextension) #1

Hello to all. I really need help!

The extension is all depended on the context menu feature. But it’s available and working only on fresh install. After Firefox is closed and reopened the context menu item is gone.

manifest.json

{
"manifest_version": 2,

"name": "my full name",
"short_name": "my short name",
"version": "1.1.5",

"author": "My author name",
"description": "Some description here.",

"content_security_policy": "script-src 'self'; object-src 'self'; media-src 'self'",

"icons": {
"16": "img/context/icon-bitty.png",
"48": "img/context/icon-small.png",
"128": "img/context/icon-large.png"
},

"browser_action": {
"default_icon": {
  "16": "img/icon_off.png",
  "32": "img/icon_off.png",
  "64": "img/icon_off.png"
},
"default_popup": "popup.html"
},

"options_page": "options.html",

"permissions": [
  "tabs",
  "activeTab",
  "contextMenus",
  "notifications",
  "<all_urls>"
],

"background": {
  "persistent": false,
  "scripts": ["js/jquery-3.2.1.min.js", "js/common.js", "js/background.js"]
},

"content_scripts": [
  {
    "matches": ["<all_urls>"],
    "css": ["css/content-style.css"],
    "js": ["js/jquery-3.2.1.min.js", "js/content-script.js"],
    "match_about_blank": true,
    "all_frames" : true,
    "run_at" : "document_end"
  }
],

"web_accessible_resources": [
  "options.html"
]
}

Since it’s a cross-browser extension I have this in my common.js

var extension = window.msBrowser || window.browser || window.chrome;

Then in my background.js

extension.runtime.onInstalled.addListener(function() {
  extension.contextMenus.create({
    id: contextMenuApiID,
    title: cmName,
    enabled: isEnabled,
    contexts: ["all"]
  });
});

extension.contextMenus.onClicked.addListener(function(data, tab) {
  // Some stuff here
});

What am I missing do doing wrong?

And one more thing: What should I do to bring the Context Menu item to all users which have already installed the extension?

Please help.


(erosman) #2

Why are you adding the contextMenus on onInstalled?

onInstalled is meant for installation time, not restart.

You need to add the contextMenus without the runtime.onInstalled.addListener


(Pcloudextension) #3

Thank you! I suppose that on the next extension update, the context menu item will be available for old users too because the background script will be “refreshed”?


(erosman) #4

Background script run every time addon is started.

If older users upgrade to the version that has the background script, it will run and the contextMenu will show, It has nothing to do with old users. Every user (old or new) that is running that addon, with that background script, will have that contextMenu.

onInstalled is used for one-off actions on new installation/upgrade when for example the developer wants to show a notice etc.

Regular addon features like contextMenu should not be set as onInstalled


(T. H. Doan) #5

This does indeed appear to be a bug, and I have filed the bug report here:

Google’s sample code for contextMenu shows them creating the context menu from onInstalled (see bug report), and it is likely the method most Chrome extension developers who read Google documentation are using to create their extension’s context menu. I’m a Chrome extension developer and I can confirm that the context menu persists across browser sessions (i.e., after a restart) for all of my Chrome extensions even though I’m creating the context menu inside the onInstalled listener. Not with Firefox though, and I realized this after porting over one of my Chrome extensions and getting service tickets for missing context menu after Firefox is restarted.

Firefox’s implementation of onInstalled is technically correct, but the fact remains that it’s not consistent with Chrome’s implementation of onInstalled (at least when it applies to context menus).