chrome sync storage to store and update array

不羁岁月 提交于 2020-06-22 11:42:07

问题


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!!


回答1:


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");
        });
    });


来源:https://stackoverflow.com/questions/15717334/chrome-sync-storage-to-store-and-update-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!