我希望我的chrome扩展程序的行为能够使用户单击扩展程序按钮时发送一条消息,然后关闭弹出窗口。我什么都不想显示在弹出窗口中,理想情况下甚至不希望它首先出现在弹出窗口中。
一半时间,我的代码有效。另一半时间,即使在发送消息后,仍然留有很少的空白气泡。为什么会出现这种不确定性?有没有一种方法可以简单地在单击扩展按钮并绕过无意义的弹出窗口时发送消息?
这是我的popup.js:
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
// Go immediately to background script.
// This popup's thread of execution will get killed as soon as we move to another tab.
chrome.extension.sendRequest({
tab: tabs[0],
message: "Button was clicked"
});
window.close();
});
包含在以下popup.html中:
<head>
<script src="popup.js"></script>
</head>
<body>
</body>
我的manifest.json
(删除了不相关的字段)是:
{
"name": "My Extension",
"description": "This is my extension",
"content_scripts": [
{
"matches": ["http://*/*", "https://*/*"],
"js": ["content.js"]
}
],
"background": { "scripts": ["background.js"] },
"browser_action": {
"default_popup": "popup.html"
},
"permissions": [
"http://*/*",
"https://*/*",
"tabs"
],
}
您刚刚default_popup
在中进行了声明manifest.json
,这意味着一旦单击浏览器操作,popup.html
就会显示。如果你不希望出现这种情况,只是删除该字段,听chrome.browserAction.onClicked
在background.js
。
manifest.json
{
...
"browser_action": {},
...
}
background.js
chrome.browserAction.onClicked.addListener(function(tab) {
chrome.runtime.sendMessage({tab: tab, message: "Button was clicked"});
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句