Firefox Webextension - TypeError: browser.contextMenus is undefined
Asked Answered
H

1

6

I refer to https://developer.mozilla.org/en-US/Add-ons/WebExtensions/API/contextMenus/onClicked. I have used the code in this example for testing in my background-script.js but this code seems not to work properly like so much other webextension code.

manifest.json

{
  "description": "Description ...",
  "manifest_version": 2,
  "name": "open-my-page",
  "version": "1.0",
  "homepage_url": "https://github.com/",
  "icons": {
    "48": "icons/page-48.png"
  },
  "background": {
    "scripts": ["background.js"]
  },
  "browser_action": {
    "default_icon": "icons/page-32.png"
  },
  "content_scripts": [{
    "matches": ["<all_urls>"],
    "js": ["/content-script.js"]
  }],
  "permission": [
    "activeTab",
    "contextMenus",
    "notifications",
    "tabs",
    "<all_urls>"
  ],
  "content_security_policy": "script-src 'self' https://api.github.com; object-src 'self'; img-src 'self'"
}

background-script.js

console.log('File: background.js');

browser.contextMenus.create({
  id: "click-me",
  title: "Click me!",
  contexts: ["all"]
});

browser.contextMenus.onClicked.addListener((info, tab) => {
  console.log("Item " + info.menuItemId + " clicked " +
              "in tab " + tab.id);
});

content-script.js

console.log('File: content-script.js');

Result:

TypeError: browser.contextMenus is undefined

I'm using Firefox 52.0.1.

Someone an idea?

Homothermal answered 24/3, 2017 at 15:22 Comment(5)
Have you tried chrome.contextMenus? Does that one work?Cunning
Please edit the question to be on-topic: include a minimal reproducible example that duplicates the problem. For Chrome extensions or Firefox WebExtensions this almost always means including your manifest.json and some of the background, content, and/or popup scripts/HTML. Questions seeking debugging help ("why isn't this code working the way I want?") must include: (1) the desired behavior, (2) a specific problem or error and (3) the shortest code necessary to reproduce it in the question itself. Please also see: What topics can I ask about here?, and How to Ask.Warplane
In particular a full manifest.json is needed. The issue could be a result of other things in the manifest. When attempting to duplicate, our needing to make any assumptions as to the contents of your code could invalidate our attempts to do so. Please include enough in the question so we can just copy and paste what is in the question into a new directory, load the extension in a new profile and duplicate the problem. It's best if you actually test doing so to verify that you have enough in the question to duplicate.Warplane
@Cunning Although I use Firefox and trying to understand webExtensions as such I've tried after you question but it's the same result: TypeError: chrome.contextMenus is undefined.Homothermal
@ Makyen Okay, I've added additional information.Homothermal
C
4

In your manifest.json file, it should be permissions not permission.

Cunning answered 25/3, 2017 at 17:38 Comment(3)
Did it work for you? This is the only issue I found in your code.Cunning
Ran into a similar issue when I accidentally defined permissions twice in the manifest, but didn't notice it m(Managua
Good find. I'm happy to find any WebExtension info/examples.Forbade

© 2022 - 2024 — McMap. All rights reserved.