Why is chrome.browserAction.onClicked undefined?

Viewed 34614

I'm writing a Chrome extension that will redirect me to a URL when clicking on the browser action icon.

I'm trying to use:

chrome.browserAction.onClicked.addListener

but I get

Uncaught TypeError: Cannot read property 'onClicked' of undefined

This is my manifest file:

{
    "name": "first extension",
    "version": "2.2.12",
    "description": "redirct to a link icon",
    "browser_action": {
        "default_icon": "icontest.png",
        "default_title": "Do action"
    },
    "permissions": ["tabs", "http://*/*"],
    "content_scripts": [{
        "matches": ["http://*.twitter.com/*", "https://*.twitter.com/*"],
        "js": ["twterland.js"]
    }],
    "icons": {
        "16": "icontest.png",
        "48": "icontest.png",
        "128": "icontest.png"
    }
}

This is my js file:

chrome.browserAction.onClicked.addListener(function(tab) { alert("hi"); });
7 Answers

The same problem may appear if you are using manifest_version 3. In this case

  • "background.scripts" should be replaced with "background.service_worker"
  • "browser_action" should be replaced with "action"
  • in js code chrome.browserAction should be replaced with chrome.action

detailed information could be found here: Manifest version 3 migration documentation

Please notice that you can heave only one of app, browser_action, page_actions present in your manifest‎.json file.

For example, to use the chrome.browserAction.setBadgeText you should have the browser_action field in your manifest‎.json.

Make sure we don't have jquery.js before background.js in the scripts array of background in manifest.json.

"background": {
    "scripts": ["background.js","jquery.js"]
}
Related