Context menu not printing anything to the console

I have an addon that is meant to just print out “hi!” when a link is right-clicked (and the menu optional is clicked). However for some reason it seems to not print anything, both in the Browser Console, and the actual tab’s console (Ctrl + Shift + I)

// manifest.json
{
  "manifest_version": 2,
  "name": "hello-world",
  "version": "1.0.0",
  "description": "The addon that does jack sh*t!",
  "permissions": [
    "contextMenus"
  ],
  "background": {
    "scripts": [
      "./background.js"
    ]
  },
  "content_scripts": [
    {
      "matches": [
        "<all_urls>"
      ]
    }
  ]
}
// background.js
const MENU_ID = 'thing-e-roo';

browser.contextMenus.create({
    id: MENU_ID,
    title: 'Perform a useless console log',
    contexts: ['link']
})

browser.contextMenus.onClicked.addListener((info, tab) => {
    if (info.menuItemId === MENU_ID) {
        console.log('hi!')
    }
})

This is the code, and it’s meant to just print “hi!” when the option is clicked. I’ve tried it without the condition, tried using alert instead and still seems to not do anything, so I’m kind of unsure what to do at this point

Thanks! :slight_smile:

I think you are looking at the wrong console, try to put console log in your background script when it executes first time to see if you can actually see it :slight_smile:.

In any case, what you want is to open:
about:debugging#/runtime/this-firefox

And then click “Inspect” button.

Or use the web-ext tool, it can now open it for you on start:

web-ext run --devtools
1 Like

Oh, huh, you’re right, this was the wrong console :sweat_smile:
In that case, how do I get it to print something in the console of the tab where the menu is used? Like if I use it on YouTube, for example, then it should show on that tab’s console
Or is it doing this because I’m using it in about:debugging, and it’s because it’s not an actual addon?

The background script is basically an invisible page loaded in the background, so whatever runs there is not visible in other consoles.
See also:

If you want to print something into the console in a specific tab, then your script needs to run inside that tab.

So if you want to see it on the YouTube page, you need to run script in the YouTube page. For example by injecting a content script there (you already have “content_scripts” in your manifest file, although you are not loading any file yet.

2 Likes