问题
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"?
回答1:
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);
});
来源:https://stackoverflow.com/questions/23270282/how-to-push-multiple-values-to-a-chrome-storage-local-array-key