8

is it possible to store array in chrome storage sync and retrieve them ?

var uarray = [abc,def,ghi];

Is it possible to update the stored array in the storage ?

var tobeadded = jkl;
uarray.push(tobeadded);

this was the syntax in documentation

chrome.storage.sync.set({'value': theValue}, function() {
    // Notify that we saved.
    message('Settings saved');
});

My bookmark extension, need to store the id of bookmark and retrieve them for internal search and stuffs based on it. bookmarking needs update of ID in storage sync periodically.

Thanks!!

Gomathi Sankar
  • 217
  • 1
  • 3
  • 15
  • Call chrome.storage.sync.set every time you modifiy the array and update the array in chrome.storage.onChanged if necessary. Also note throttling limits. – 方 觉 Mar 30 '13 at 10:33
  • update should be directly added to storage similar to push in Jscript, anything i can use to add value to existing array in storage ?, i won't be able to create array with old values + New value to be stored. – Gomathi Sankar Mar 30 '13 at 11:19

1 Answers1

7

You can read the existing values, append the new value and store back.

Following sample code should allow you to add newArrEntry into existing array stored in chrome.storage.sync

chrome.storage.sync.get(["storagekey"], function(result) {
        var array = result[storagekey]?result[storagekey]:[];

        array.unshift(newArrEntry);

        var jsonObj = {};
        jsonObj[storagekey] = array;
        chrome.storage.sync.set(jsonObj, function() {
            console.log("Saved a new array item");
        });
    });
Nirmal Patel
  • 5,128
  • 8
  • 41
  • 52