Domanda

I'm using the following code to set a key:value to the local storage:

chrome.storage.local.set({"key": value}, null);


What can I do to add multiple values to the key "key"?

È stato utile?

Soluzione

First use the get method and then use set inside the get callback to add your new storage data as a key/value pair to the storage object returned from get. Example:

chrome.storage.local.get(function(cfg) {
  if(typeof(cfg["key"]) !== 'undefined' && cfg["key"] instanceof Array) { 
    cfg["key"].push("value");
  } else {
    cfg["key"] = ["value"];
  }
  chrome.storage.local.set(cfg); 
});
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top