4

Problem
From within a Chrome extension's popup page, using any JavaScript method to cause a window popup results in the popup window animating but, as soon as the content of the window is populated, instantly losing focus and disappearing.

The weird thing is that, the popup window will stay open as long as Chrome's developer tools are opened in the context of the extension, but the popup window will immediately disappear if the developer tools are closed.

 

This used to happen on my old, messed-up 2006 iMac, so I thought it was an issue with my configuration. However, I've just done a fresh install of both Mac OS X (v1.7.5) and Chrome (v23.0.1271.97) on a 2007 iMac and created a very simple Chrome extension (code below) to test and it's still happening. I can't help but think this is a bug with Chrome.

 

Code

"manifest.json"

{
    "name": "Test Popups",
    "version": "0",

    "manifest_version": 2,

    "browser_action": {
        "default_popup": "popup.html"
    }
}

"popup.html"

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title></title>

        <script src="popup.js"></script>
    </head>

    <body>
        <input type="submit" id="btn_Alert" value="Alert" />
        <input type="submit" id="btn_Confirm" value="Confirm" />
        <input type="submit" id="btn_Prompt" value="Prompt" />
    </body>
</html>

"popup.js"

document.onreadystatechange = function(){
    if (document.readyState === "complete"){
        attach_eventListeners();
    }
}

function attach_eventListeners(){
    var btn_Alert = document.getElementById("btn_Alert");
    var btn_Confirm = document.getElementById("btn_Confirm");
    var btn_Prompt = document.getElementById("btn_Prompt");

    btn_Alert.addEventListener("click", function(){
        window.alert("Test");
    }, false);
    btn_Confirm.addEventListener("click", function(){
        window.confirm("Test");
    }, false);
    btn_Prompt.addEventListener("click", function(){
        window.prompt("Test", "");
    }, false);
}

Here's a link to a ZIP of these files. To test them yourself:

  1. Extract the ZIP
  2. In Chrome, go to chrome://chrome/extensions/
  3. Check the "Developer mode" checkbox in the top right
  4. Click "Load unpacked extension..." in the top left
  5. Select the folder containing the extracted files
  6. Click "OK"
4

1 回答 1

3

As far as I understand, popup windows cannot lose focus, or they are immediately closed. Having developer tools open for inspecting overrides this policy.

Using alert, confirm, prompt causes the popup window to lose focus and therefore it is closed by Chrome.

于 2014-05-09T09:33:42.413 回答