Zeynel
Zeynel

Reputation: 13515

How do I save user email as a variable when someone installs the extension?

In my bookmarking extension, I need to send the gmail address of the user to google app engine in order to write the bookmark to the database as the user as "owner".

My understanding is that I cannot use a popup because I have a background page (I remember reading about this but I could not find it again). I am also reading the installation process in Chrome store. I would appreciate if anyone can direct me to the right place in the documentation.

I copy my background.html below with the variable extension_user included. How do I get this variable from the user when they upload the extension? This is my previous question.

<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);
//***the variable with user email to send to backend:***//
//formData.append("extension_user", extension_user)

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>

Upvotes: 0

Views: 428

Answers (1)

David
David

Reputation: 13999

You can use both a popup and background page in a single extension. A lot of my extensions use both... Use the background page to communicate and save data for your popup page...

You can prompt your user to save their email address on install in your background page as follows:

<script type="text/javascript">
  addEventListener('load', function(){      
    var MAJOR_VERSION=1.0;
    if(!localStorage.updateread||localStorage.updateread!=MAJOR_VERSION)
      {  
        var email=prompt("Enter the Email Address : ")
        localStorage["Email"] = Email;
        localStorage.updateread=MAJOR_VERSION  
      } 
  }, 0);
</script>

This script will only run when you first install the extension. And the users email address will be saved in the extensions LocalStorage until they uninstall... Calling this variable will now work in both your background page and your popup page...

Upvotes: 2

Related Questions