chrome.storage.local.get and set [duplicate]

This code works for me:

function load() {
    var channels = "";
    var keywords = "";
    chrome.storage.local.get('channels', function (result) {
        channels = result.channels;
        alert(result.channels);
        $("#channels").val(channels);
    });
} 

Chrome.storage.local.get() returns an object with items in their key-value mappings, so you have to use the index of the key in your search pattern.

IMP:

Thanks to Rob for identifying: Chrome.storage.local.get() is asynchronous, you should modify your code to ensure they work after callback() is successful.

Let me know if you need more information.


debug or use

alert(JSON.stringify(result));

for more details as to what you are getting back


The "result" value you are using is an object that contains the storage value, to get the value you have to use result.keywords, which will get the value of the keywords. EX:

function load(){
  chrome.storage.local.get('keywords', function(result){
    var keywords = result.keywords;
    alert(keywords);
  });

  chrome.storage.local.get('channels', function(result){
    var channels = result.channels;
    alert(channels);
  });
}