How can I keep the value of variable after reload browser in chrome extensions

I am writing my first chrome extension and I am kind of new in this field.

My goal is sending url and email to a PHP server, but for now I send them to console.

My problem is saving the last email that user has entered to my extension. Right now when I close the browser the value of email will change to undefined.

I put entire code of my extension, but I think the problem must be in getemail function.

background.js

//variables defenitions:
var email;
var urll;
//functions:

function getemail(){
     email = window.prompt("Pleas  enter your Email address:");
     chrome.storage.sync.set({'email':email}, function() {
      alert('You entered \n[' + email + "]\n as Email address.\nThank you for registration." );
      });
        var id = chrome.contextMenus.create({"title": "Submit your url","onclick":consollog });
        var id = chrome.contextMenus.create({"title": email+"/Change" ,"onclick":getemail });
        chrome.contextMenus.remove("1", null);
        chrome.contextMenus.remove("2", null);
        var id = chrome.contextMenus.create({"id":"2","title": "SUPPORT","onclick":support});
     }


function create(){
        var id = chrome.contextMenus.create({"title": "Submit your url","onclick":consollog });
        var id = chrome.contextMenus.create({"title": email+"/Change" ,"onclick":getemail });
        chrome.contextMenus.remove("1", null);
        chrome.contextMenus.remove("2", null);
        var id = chrome.contextMenus.create({"id":"2","title": "SUPPORT","onclick":support});
     }
function support(){
    alert("Contact ways:\nPhone number:+989378114692\nEmail address:[email protected]");
  }

function consollog(info, tab) {
  chrome.tabs.query({
  'active':true,"currentWindow": true,
}, function(lTabs) {
    urll=lTabs[0].url;
  console.log(urll)
  console.log(email)
  alert('You sent "' + urll + '" to our server');
});
}
//menu items:
if(email==undefined){
   var id = chrome.contextMenus.create({"id":"1","title": "Register Email","onclick":getemail });
    } 
   else {create();}


var id = chrome.contextMenus.create({"id":"2","title": "SUPPORT","onclick":support});

manifest.json:

{
   "name": "Jenco",
   "description": "This extension helps you to send url",
   "version": "0.9",
   "browser_action":{
      "default_icon":"32.png",
      "default_popup":"mypopup.html"},
   "permissions": ["contextMenus","tabs","activeTab", "http://*/*", "https://*/*","storage"],
   "background": {
      "scripts": ["background.js"]
      },
   "manifest_version": 2,
   "icons": { "16": "16.png",
              "32": "32.png",
              "48": "48.png" },
   "commands": {
      "send-url": {
         "suggested_key": { "default": "Ctrl+Shift+Z" },
         "description": "Send an url"
                    }
                }
}

Upvotes: 1

Views: 2887

Answers (1)

Ebbez
Ebbez

Reputation: 364

My codes to save a variable and to load a variable:

// Saving
chrome.storage.sync.set({"variableName": value});
// Loading 1 thing
chrome.storage.sync.get("variableName", function(result){
    // Showing the requested variable value
    alert(result.variableName);
});
// Loading more things
chrome.storage.sync.get(["variableName", "variableNameTheSecond"], function(result){
    // Showing first the first one and then the second one
    alert(result.variablename);
    alert(result.variableNameTheSecond);
});

I hope this helps you, greets

Ebbe

Upvotes: 2

Related Questions