chrome同步存储以存储和更新阵列

Gom*_*kar 8 google-chrome-extension google-chrome-storage

是否可以将数组存储在chrome存储同步中并检索它们?

var uarray = [abc,def,ghi];
Run Code Online (Sandbox Code Playgroud)

是否可以更新存储中存储的数组?

var tobeadded = jkl;
uarray.push(tobeadded);
Run Code Online (Sandbox Code Playgroud)

这是文档中的语法

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

我的书签扩展,需要存储书签的ID并检索它们以进行内部搜索和基于它的填充.书签需要定期更新存储同步中的ID.

谢谢!!

Nir*_*tel 7

您可以读取现有值,附加新值并存储回来.

以下示例代码应该允许您添加newArrEntry到现有的array存储中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");
        });
    });
Run Code Online (Sandbox Code Playgroud)