Chrome扩展程序:popup.html关闭并在新标签页中打开响应?

时间:2011-05-17 12:48:20

标签: javascript ajax google-chrome-extension

我的popup.html上有一个登录表单,它调用以下函数...

 chrome.extension.sendRequest({
        req: "login",
        user: username,
        pass: pass,
        remember: remember
    }, function(response) {
        console.log("RESPONSE RECIEVED HOOORAH!");
            });

这个函数依次转到我的background.html中的以下switch语句。

do_login(request.user, request.pass, request.remember, false, true, function(response){
                        if(response == true){
                            sendResponse("success");
                        }else{
                            sendResponse("badLogin");
                        }
                    })

以下是do_login的内容。在执行do login期间,我的popup.html随机关闭并在新选项卡中重新打开,代码在那里完成,我已登录。为什么会发生这种情况?

 var xhr = new XMLHttpRequest();
    xhr.open("GET",requrl,true);
    xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
    xhr.onreadystatechange = function(do_login){
        if(xhr.readyState == 4){
            if(xhr.status == 200){
                console.log(xhr.responseText);
                try{
                    //Incase they were just logged in as another user.
                    createContextMenu();
                    //users info.
                    var resp = JSON.parse(xhr.responseText);
                    if(resp.default === void(0)){
                        logOut();
                        callback(null);
                    }
                default = resp.default;

                for(var i=0;i<resp.s.length;i++){
                globalStaks[i] = resp.s[i];
                }
                st = global;
                bud = resp.bud;
                msg = resp.msg;
                stakid = resp.default;
                }catch(x){
                // This situation is where you have incorrect password
                console.log("something is wrong with logging in ")
                clearLoginInfo();
                console.log("Incorrect password");
                }
                if(resp.msg == "denied")
    {
        clearLoginInfo();
        callback(false);
    }
    else
    {
        loggedIn = true;
        user = username;
        userid = resp.userid;
        if(refresh){
            refreshpage();
        }
        if(notificationdisplay){
            notification.cancel();
        }
        if(remember)
        {
            clearLoginInfo();
            storeLogin(username,pass);
        }
        localStorage.setItem("pass",pass);
        md5 = pass;
        callback(true);
        }
        }else {
            callback(false);
        }
    }
}
        xhr.send(null);

修改

它显示为收到的最后一个错误background.html抛出.. Attempting to use a disconnected port object

完整的追踪是......

 Uncaught Error: Attempting to use a disconnected port object
 chrome.Port.postMessagechrome/RendererExtensionBindings:147
 chromeHidden.Port.dispatchOnConnect.connectEventchrome/RendererExtensionBindings:89
 sendResponse.reply background.html:1266
 xhr.onreadystatechange

1 个答案:

答案 0 :(得分:0)

弹出窗口中的代码和数据是短暂的,因为只要弹出窗口关闭,关联的代码就会关闭,因此从后台页面到弹出窗口的任何回调都将失败(没有任何回调)

您需要找到打开新页面的内容(并非所有代码都包含在问题中 - createContentMenu会做什么?)。桌面通知可能会导致您失去焦点,从而可能关闭弹出窗口。

还要确保在弹出窗口中捕获表单提交,然后通过XMLHttpRequest传递请求(或使用嵌入式imframe来托管表单),因为弹出窗口中的表单提交无法正常工作(您的页面不会在弹出窗口内更新。