Cannot read property 'onClicked' of undefined when using chrome.action or chrome.browserAction

google-chrome-extension, javascript

Solution

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.

Problem

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

Original source