Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
930 views
in Technique[技术] by (71.8m points)

chrome sync storage to store and update array

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...