How To Set A Simple Dynamic Variable Into Chrome.storage.local.set For A Chrome Extension?
I have a slider and I want to be able to set the value into local storage so that I can use this value dynamically on a web page. The part I need help with is chrome.storage.local.
Solution 1:
You can use this way:
// Setvar dataObj = {};
dataObj["key1"] = "vaule1";
// dataObj["key2"] = "vaule2"; // if you want to set value more than one key.
chrome.storage.local.set(dataObj, function() {
if(!chrome.runtime.lastError){
// set storage value successfully.
}
});
// Getvar dataObj = ["key1"];
// var dataObj = ["key1", "key2"]; // if you want to get value more than one key.
chrome.storage.local.get(dataObj, function (callback){
if(callback && callback["key1"]){
// To Do: handle callback["key1"].
}
});
// Removevar dataObj = ["key1"];
// var dataObj = ["key1", "key2"]; // if you want to remove value more than one key.
chrome.storage.local.remove(dataObj, function(callback) {
if(!chrome.runtime.lastError){
// remove successfully.
}
});
To learn more, see chrome.storage
Solution 2:
You may need to pay a little more attention to the api. chrome.storage.local is of type StorageArea. As such, the first (and only mandatory) argument to chrome.storage.local.set
is “An object which gives each key/value pair to update”. You’re only giving the second half of that object. Try:
chrome.storage.local.set({"sidemargins":v});
Post a Comment for "How To Set A Simple Dynamic Variable Into Chrome.storage.local.set For A Chrome Extension?"