如何测试用户是否已注册?

时间:2011-10-23 14:32:52

标签: javascript google-chrome-extension

我正在使用Chrome扩展程序,而background.html我想测试用户是否已注册,如果没有触发popup.html并获取用户电子邮件。这就是我想要做的事情:

var extension_user = "";

if (extension_user)
{
    //run normal program:
    //...
    formData.append("extension_user", extension_user);
    //...
}
else
{
    //trigger popup.html
    //get user_email from form in popup.html
    //change extension_user to user_email  
}

我遇到的问题是,下次此程序运行时extension_user再次设置为空字符串,popup.html将被触发。如何设置程序流程,以便仅当extension_user为空字符串时才运行else子句?我把下面的background.html包括在内。感谢

background.html

<html>
<script>
chrome.browserAction.onClicked.addListener(function(tab) 
{
    chrome.tabs.getSelected(null, function(tab) 
    {
        // Send a request to the content script.
        chrome.tabs.sendRequest(tab.id, {action: "getDOM"}, function(response) 
        {
            var firstParagraph = response.dom;
            var formData = new FormData();
            formData.append("url", tab.url);
            formData.append("title", tab.title);
            formData.append("pitch", firstParagraph);
            formData.append("extension_user", "azeynel1@gmail.com");

            var xhr = new XMLHttpRequest();
            xhr.open("POST", "http://ting-1.appspot.com/submithandlertest", true);
            xhr.onreadystatechange = function (aEvt) 
            {
                if (xhr.readyState == 4) 
                {
                    if (xhr.status == 200)
                    { 
                        console.log("request 200-OK");
                        chrome.browserAction.setBadgeText ( { text: "done" } );
                        setTimeout(function () 
                        {
                            chrome.browserAction.setBadgeText( { text: "" } );
                        }, 2000);
                    }
                    else
                    {
                        console.log("connection error");
                        chrome.browserAction.setBadgeText ( { text: "ERR" } );
                    }
                }
            };
            xhr.send(formData);
        }); //chrome.tabs.sendRequest
    });
});

</script>
</html>

1 个答案:

答案 0 :(得分:1)

如果您正在寻找一种在浏览器重启之间存储注册用户的方法,那么就有localStorage

此外,无法以编程方式打开浏览器操作弹出窗口,我宁愿打开选项页面并提示用户登录。