Javascript 为什么 chrome.browserAction.onClicked 未定义?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11494232/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 06:02:53  来源:igfitidea点击:

Why is chrome.browserAction.onClicked undefined?

javascriptgoogle-chrome-extension

提问by Alon Mahl

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

我正在编写一个 Chrome 扩展程序,当点击浏览器操作图标时,它会将我重定向到一个 URL。

I'm trying to use:

我正在尝试使用:

chrome.browserAction.onClicked.addListener

but I get

但我明白了

Uncaught TypeError: Cannot read property 'onClicked' of undefined

未捕获的类型错误:无法读取未定义的属性“onClicked”

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:

这是我的js文件:

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

采纳答案by u283863

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

代码似乎在您的twterland.js文件中,这是您的内容脚本。browserAction只能在扩展页面中使用,因此您不能在内容脚本中使用它。

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

文档: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

但是,内容脚本有一些限制。他们不能
-使用 chrome.* API(chrome.extension 的部分除外)
- 使用由其扩展页面
定义的变量或函数 - 使用由网页或其他内容脚本定义的变量或函数

Put it on the background pageinstead.

把它放在背景页面上

回答by Kirill Oficerov

For those who already have added something like

对于那些已经添加了类似的东西的人

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

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

并且仍然得到Cannot read property 'onClicked' of undefined- 只需添加

"browser_action": {}

into your manifest.json

进入你的 manifest.json

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

编辑:感谢@Pacerier 的评论,我已经改变了我的答案

回答by Sgnl

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

如果您没有"browser_action"定义属性,manifest.json则可能会发生此错误。@Kirill 的答案有效,但您还必须添加一个空白icon.png文件,否则 chrome 会抛出一个错误,提示它找不到这样的文件。

Adding this to the manifest.jsonfile shouldsuppress this is error:

将此添加到manifest.json文件应该抑制这是错误:

"browser_action": {}

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

请务必阅读文档以获取有关如何使用该"browser_action"设置的进一步参考

回答by Fergal Moran

I was also getting this, adding

我也得到了这个,补充说

"persistent": true 

to my background declaration in manifest.json solved it.

我在 manifest.json 中的背景声明解决了它。

回答by Vinayak Bansal

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

确保我们在 manifest.json 中的 background 脚本数组中的 background.js 之前没有 jquery.js。

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