单击Chrome扩展程序浏览器操作按钮时不显示弹出窗口

时间:2016-08-21 21:31:10

标签: google-chrome-extension javascript-events

我希望我的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"
  ],
}

1 个答案:

答案 0 :(得分:1)

您刚刚在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"});
});
相关问题