1

I am working on a Chrome extension and in the background.html I want to test if the user is registered and if not trigger the popup.html and get the user email. This is what I want to do:

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  
}

The problem I am having is that the next time this program runs extension_user is again set to an empty string and popup.html will be triggered. How do I set up the flow of the program so that else clause is run only if extension_user is an empty string? I incude the background.html below. Thanks

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", "[email protected]");

            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 Answer 1

1

If you are looking for a way of storing registered user between browser restarts then there is localStorage.

Also there is no way of opening browser action popup programmatically, I would rather open options page and prompt user to login there.

8
  • Thanks. I am reading about both localStorage and Options page. But as mentioned in this answer stackoverflow.com/questions/7830209/… I need to verify that the email the user submits to the extension is his own email. So I thought about asking the user to log in to the app first. This is my other question about the same issue: stackoverflow.com/questions/7880334/…
    – Zeynel
    Oct 24, 2011 at 19:14
  • Ok, I am trying to do this with the options page. How do I prompt the user to log in there? If I use a form to get his email, I think, then I need to send it to the app and back. What am I missing? Thanks again.
    – Zeynel
    Oct 24, 2011 at 21:54
  • ok, but when you wrote "open options page and prompt user to login there" is about the extension. I created the options page as explained in the documentation code.google.com/chrome/extensions/options.html how do I prompt the user to login in the options page?
    – Zeynel
    Oct 25, 2011 at 0:21
  • @Zeynel Options page is a regular html page where you can do whatever you like. You can create form there, you can send ajax requests from it, anything. Whatever is required for google app login you can prompt there (I don't know what that is).
    – serg
    Oct 25, 2011 at 0:29
  • I am sorry, my mistake, in this case, I am trying to handle this without going to app engine. For instance, can I prompt for user email in the options page and write it to localStorage so that I can have the background page use that email?
    – Zeynel
    Oct 25, 2011 at 0:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.