Search code examples
javascriptfirefoxfirefox-addonfirefox-addon-webextensions

browser.menus.onClicked.addListener not working in Firefox


Currently I'm working on my own Firefox extension and I've met the problem with adding listener to an onclick event for a context menu item.

manifest.json

{
  "manifest_version": 2,
  "name": "My extension name",
  "version": "1.0",
  "description": "My extension description",
  "icons": {
    "48": "icons/icon.png"
  },
  "permissions": ["menus"], 
  "background": {
    "scripts": ["index.js"]
  }
}

index.js

browser.menus.create({
  id: 'my-ext-item',
  title: 'Custom ctx item',
  contexts: ['selection']
});

browser.menus.onClicked.addListener(function(info, tab) {
  console.log("Clicked!");
});

browser.menus.create() apparently works fine, because the new item appear in my context menu. The problem is with catching a click event - it is never fires.

I've wrote above code according to the MDN Web Docs. I test it on Firefox 97.0.1 x64.

What did I wrong and what should I repair?

PS. I was trying with older browser.contextMenus.create and browser.contextMenus.onClicked.addListener but it didn't work either.


Solution

  • I've found a solution - browser.menus.onClicked.addListener() working properly, just there is a necessity to enable logging in browser settings.

    First, go to about:config and find key extensions.logging.enabled and switch it to true. Then, display Browser Console from Menu Bar -> Tools -> Browser Tools -> Browser Console or by shortcut Ctrl+Shift+J.

    Be aware that Browser Console is not the same as Firefox Developer Tools (from F12 or Ctrl+Shitft+I)!

    Last, but not least enable Show Content Messages in Browser Console

    enter image description here