Ebbez
Ebbez

Reputation: 364

How to put the Chrome storage data in a variable?

I tried to put some stuff in the local storage, but when I try to put the information of the local storage in a variable it says "undefined". So here is my code:

var data = {};
chrome.storage.local.get(["murl", "musername", "mpassword"], function(result){
    data["url"] = result.murl;
    data["username"] = result.musername;
    data["password"] = result.mpassword;
    alert(result.musername);
});
alert(data["username"]);

It first shows: undefined and then it shows Ebbez, also strange is when I remove the last line, I get Ebbez.

Upvotes: 0

Views: 924

Answers (1)

Ebbez
Ebbez

Reputation: 364

To store information you need the chrome.storage functions, here are some examples.

// Save 1 data item
chrome.storage.sync.set({"variableName":value});

// Save multiple data items
chrome.storage.sync.set({"variableName":value, "secondVariableName":secondValue});

// Load 1 data item
chrome.storage.sync.get("variableName", function(result){
    // Shows variable
    alert(result.variableName);
});

// Load multiple data items
chrome.storage.sync.get(["variableName", "secondVariableName"], function(results){
    // Shows multiple variables
    alert(results.variableName);
    alert(results.secondVariableName);
});

Upvotes: 1

Related Questions