我正在尝试编写一个非常简单的 Chrome 扩展程序。此时只有一个弹出的 html 文件,当点击浏览器操作图标时,它会尝试显示警报。我显然做错了什么,因为警报没有触发。
清单.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>
<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>
我也试过:
<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>
基于响应的更新 感谢您的响应。我进行了以下更改,但仍未被调用 browser.Action.onClicked() (您可以看到,我正在使用 console.log() 而不是警报。(显示全局范围的日志,内部的日志)回调不是)。
清单.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>
<head>
<script type="text/javascript" src="popup.js"></script>
</head>
<body>
<div style="white-space: nowrap">
Hello World!
</div>
</body>
</html>
popup.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()");
});