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>