开发者

Google chrome extension issue: popup.html interferes with script execution in background.html

I am learning how to extend Google chrome and I have run into the following problem:

I have the following manifest file:

{
    "name": "My First Extension",
    "version": "1.0",
    "description": "The first extension that I made.",
    "background_page": "background.html",
    "browser_action": {
        "default_icon": "icon.png", 
开发者_如何学运维        "popup": "popup.html"
     },
    "permissions": [
    "tabs",
        "http://*/*",
        "https://*/*"
    ]
 }

My background.html file just injects some simple JavaScript into the page:

<script>
    // Called when the user clicks on the browser action.
    chrome.browserAction.onClicked.addListener(function(tab) {
        chrome.tabs.executeScript(null, {code:"alert(\"hi from background CODE\");"});
    });
</script>

My popup.html file is just simple HTML:

<body>
    Sup Playa
</body>

The dialog box from background.html never gets displayed. popup.html functions as expected.

However when I comment out popup.html from the manifest file, the script in background.html works.

What am I doing wrong? Why aren't both the dialog box and the popup showing up?


As it says in the docs:

onClicked event will not fire if the browser action has a popup.

So if you assign any html file to your popup "popup": "popup.html" (rather than just a button without body), onClicked event isn't fired.

You can just put your code right into popup.html (it has same privileges as a background page) if you want something to be executed each time it is opened:

chrome.tabs.executeScript(null, {code:"alert(\"hi from background CODE\");"});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜