73

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"); });
0

8 Answers 8

181

ManifestV3

manifest.json: add action (not browser_action), see also the migration guide.

  "action": {},
  "background": {"service_worker": "background.js"},

background.js: use chrome.action not chrome.browserAction.

Classic ManifestV2

For those who already have added something like

"background": {
    "scripts": ["background.js"]
}

and still gets Cannot read property 'onClicked' of undefined - just add

"browser_action": {}

into your manifest.json

7
  • 2
    This is actually my problem. My script was already background. It's just I have never declared any browser_action to begin with.
    – Luke Vo
    Commented Feb 11, 2017 at 7:32
  • 9
    "browser_action": {} is sufficient
    – Pacerier
    Commented Aug 7, 2017 at 15:34
  • Why????????? This makes no sense... I changed the icon to another file and it failed to execute. I change it back to icon.png and all is well! Thank you, but I am really scratching my head...
    – birwin
    Commented Sep 6, 2017 at 21:49
  • 1
    this would not work if the manifest version is 3 Commented Dec 26, 2021 at 4:05
  • 1
    Once you've done this, you also need to click "Clear all" in the Errors page to clear the previous error. Commented Nov 19, 2023 at 11:26
27

It seems like the code is in your twterland.js file, which is your content script. browserAction can only be used in extension pages, so you can not use it in content scripts.

Document: https://developer.chrome.com/extensions/content_scripts

However, content scripts have some limitations. They cannot:
- Use chrome.* APIs (except for parts of chrome.extension)
- Use variables or functions defined by their extension's pages
- Use variables or functions defined by web pages or by other content scripts

Put it on the background page instead.

1
19

If you do not have a "browser_action" property defined in your manifest.json then this error may occur. @Kirill's answer works but you also have to add a blank icon.png file else chrome will throw an error that it cannot find such a file.

Adding this to the manifest.json file should suppress this is error:

"browser_action": {}

Be sure to read the documentation for further reference on how to use the "browser_action" setting.

3
  • 2
    I have browser_action defined in my manifest, still chrome.pageAction is not defined in my background script Commented Jan 8, 2018 at 19:54
  • @AlexanderMills The orginal question was regarding chrome.browserAction so I'm not sure about pageAction.
    – Sgnl
    Commented Jan 8, 2018 at 20:15
  • 4
    I also had to remove and reinstall the extension.
    – Evanss
    Commented Jan 8, 2021 at 15:24
10

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

2
  • Thank you! This saved me - i knew about the first three but the chrome.action one is the one i didn't know about!
    – DerTarchin
    Commented May 19, 2022 at 14:09
  • Renaming browserAction to action was the missing puzzle piece, thanks.
    – tempvar
    Commented Nov 24, 2022 at 9:34
4

I was also getting this, adding

"persistent": true 

to my background declaration in manifest.json solved it.

2
  • While this "solves" the situation, there are apparently quite a few reasons not to have "persistent": true, so it isn't an ideal solution. Commented Sep 18, 2020 at 8:31
  • Note that if your Chrome extension is using Version3 in the Manifest, this feature won't exist.
    – Raleigh L.
    Commented Jan 10, 2022 at 23:44
1

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.

0

Since manifest v3, browser_action is changed to action. https://developer.chrome.com/docs/extensions/develop/migrate/api-calls#replace-browser-page-actions

So in the manifest, have the following:

"action": {},

And then you can handle the extension click with:

chrome.action.onClicked.addListener(function (tab) {...})
-3

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"]
}
0

Not the answer you're looking for? Browse other questions tagged or ask your own question.