XMLHttpRequest冻结Chrome扩展程序

时间:2014-02-11 19:36:34

标签: javascript ajax google-chrome google-chrome-extension xmlhttprequest

我无法以异步方式使XMLHttpRequest工作。

以下是我在background.js文件

中使用的代码
chrome.browserAction.onClicked.addListener(function(){
    if (!ready)
    {
        alert("Please wait, extension is not initialized yet. If you've just installed the extension, it can takes up to 10 minutes.");
    }
    else
    {
        game = urls[Math.floor(Math.random() * urls.length)];
        chrome.tabs.create({
            url: game
        });
    }
});

var xhr = new XMLHttpRequest();
xhr.open("GET", "myurl", true);
xhr.onreadystatechange = function() {
    if (xhr.readyState == 4) {
        // Some stuff
        ready = true;
        alert("Done");
    }
}
xhr.send();

这很好用。唯一的问题是在XHR完成之前不会触发browserAction事件。如果我之前尝试点击,则会在显示"Done"后触发该事件。

我做错了吗?

这是我的清单文件中唯一有趣的部分

"background": {
    "scripts": ["background.js"]
},

0 个答案:

没有答案