javascript Chrome 扩展程序:browserAction.onClicked.addListener() 未被调用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12706649/
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
Chrome Extension: browserAction.onClicked.addListener() not being called
提问by RobertJoseph
I am trying to write a very simple Chrome extension. All it is at this point is a popup html file that tries to display an alert when the browser action icon is clicked. I am obviously doing something wrong because the alert doesn't fire.
我正在尝试编写一个非常简单的 Chrome 扩展程序。此时,它只是一个弹出 html 文件,它会在单击浏览器操作图标时尝试显示警报。我显然做错了什么,因为警报没有触发。
manifest.json
清单文件.json
{
"name": "Simple",
"description": "Simple",
"version": "1.0",
"browser_action": {
"default_title": "Simple",
"default_icon": "images/icon.png",
"default_popup": "popup.html"
},
"manifest_version": 2
}
popup.html
弹出窗口.html
<html>
<head>
<script>
//Executed when the extension's icon is clicked
chrome.browserAction.onClicked.addListener(function(tab)
{
alert("gah");
});
</script>
</head>
<body>
Hello World!
</body>
</html>
I have also tried:
我也试过:
<html>
<head>
<script>
function onPageLoad()
{
//Executed when the extension's icon is clicked
chrome.browserAction.onClicked.addListener(function(tab)
{
alert("gah");
});
}
</script>
</head>
<body onload="onPageLoad()">
Hello World!
</body>
</html>
UPDATE BASED UPON RESPONSESThank you for your response. I've made the following changes but is still not being called browser.Action.onClicked() (you can see that instead of an alert I am using console.log(). (The log at global scope is displayed, the one inside the callback is not).
根据回复更新感谢您的回复。我进行了以下更改,但仍然没有被称为 browser.Action.onClicked()(您可以看到我使用的是 console.log() 而不是警报)。(显示全局范围的日志,里面的回调不是)。
manifest.json
清单文件.json
{
"name": "Simple",
"description": "Simple",
"version": "1.0",
"permissions": ["tabs", "http://*/*", "https://*/*"],
"background": {
"persistent": false,
"scripts": ["popup.js"]
},
"browser_action": {
"default_title": "Simple",
"default_icon": "images/icon.png",
"default_popup": "popup.html"
},
"manifest_version": 2
}
popup.html
弹出窗口.html
<html>
<head>
<script type="text/javascript" src="popup.js"></script>
</head>
<body>
<div style="white-space: nowrap">
Hello World!
</div>
</body>
</html>
popup.js
弹出窗口.js
console.log("Running at global scope")
//Executed when the extension's icon is clicked
chrome.browserAction.onClicked.addListener(function(tab)
{
console.log("Running insode of addListener()");
});
回答by Konrad Dzwinel
chrome.browserAction.onClicked
can't be used inside a popup. It doesn't make much sense - you start listening for browser action icon being clicked after it was already clicked. It can be clicked again, but then popup will be closed and listener will be terminated before anything happens.
chrome.browserAction.onClicked
is supposed to be used on the background page and only when popup for browser action is not used:
chrome.browserAction.onClicked
不能在弹出窗口中使用。这没有多大意义 - 您在点击浏览器操作图标后开始监听它被点击。可以再次单击它,但是弹出窗口将关闭,并且侦听器将在发生任何事情之前终止。
chrome.browserAction.onClicked
应该在后台页面上使用,并且仅在不使用浏览器操作的弹出窗口时使用:
This event will not fire if the browser action has a popup.
如果浏览器操作有弹出窗口,则不会触发此事件。
You should probably just write:
你可能应该只写:
<script>
alert("gah");
</script>
But I'm not sure if alerts work in a popup. Better try something like:
但我不确定警报是否在弹出窗口中起作用。最好尝试类似:
<script>
document.getElementsByTagName('body')[0].innerHTML = "gah";
</script>
EDIT
编辑
There is one more thing to fix in your extension. As @Cody suggested in his answer, you shouldn't have used inline script. It is blocked for security reasons. Take the code I suggested above and put it into a separate javascript file, then include it in popup.html
head:
在您的扩展程序中还有一件事需要修复。正如@Cody 在他的回答中所建议的那样,您不应该使用内联脚本。出于安全原因,它被阻止。将我上面建议的代码放入一个单独的 javascript 文件中,然后将其包含在popup.html
head 中:
<script src='script.js' type='text/javascript'></script>
回答by Apoorv Saxena
According to revised Chrome Security Policies, the chrome extensions will no longer be able to use inline javascript in your popup.html http://developer.chrome.com/extensions/contentSecurityPolicy.html#jsexecution
根据修订后的 Chrome 安全政策,chrome 扩展程序将不再能够在您的 popup.html http://developer.chrome.com/extensions/contentSecurityPolicy.html#jsexecution 中使用内联 javascript
You will have to create a JS file and state about its existence in the Manifest.json as coded below:
您必须创建一个 JS 文件并在 Manifest.json 中说明它的存在,代码如下:
manifest.json
清单文件.json
{
"name": "Simple",
"description": "Simple",
"version": "1.0",
"background": {
"scripts": ["bg.js"] //Here you can name the JS file that you have created
},
"browser_action": {
"default_title": "Simple",
"default_icon": "images/icon.png",
"default_popup": "popup.html"
},
"manifest_version": 2
}
popup.html
弹出窗口.html
<html>
<head>
<script>
//Executed when the extension's icon is clicked
alert("gah");
</script>
</head>
<body>
Hello World!
</body>
</html>
You can get started building Chrome Extension following Manifest Version 2, taking reference from http://developer.chrome.com/extensions/overview.htmland http://developer.chrome.com/extensions/getstarted.html
您可以按照 Manifest Version 2 开始构建 Chrome 扩展,参考http://developer.chrome.com/extensions/overview.html和http://developer.chrome.com/extensions/getstarted.html