Why is chrome.browserAction.onClicked undefined?

前端 未结 6 1132
情书的邮戳
情书的邮戳 2020-12-02 18:38

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.onC         


        
相关标签:
6条回答
  • 2020-12-02 18:54

    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.

    0 讨论(0)
  • 2020-12-02 18:56

    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

    edit: thanks @Pacerier for his comment, I've changed my answer

    0 讨论(0)
  • 2020-12-02 18:56

    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 讨论(0)
  • 2020-12-02 18:58

    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 讨论(0)
  • 2020-12-02 19:05

    I was also getting this, adding

    "persistent": true 
    

    to my background declaration in manifest.json solved it.

    0 讨论(0)
  • 2020-12-02 19:13

    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.

    0 讨论(0)
提交回复
热议问题