【问题标题】:chrome sync storage to store and update arraychrome 同步存储来存储和更新数组
【发布时间】:2013-03-30 10:13:35
【问题描述】:

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

var uarray = [abc,def,ghi];

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

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

这是文档中的语法

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

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

谢谢!!

【问题讨论】:

  • 每次修改数组时调用 chrome.storage.sync.set 并在必要时更新 chrome.storage.onChanged 中的数组。另请注意节流限制。
  • 更新应该直接添加到存储中,类似于 Jscript 中的推送,我可以用什么来为存储中的现有数组添加值?,我将无法使用旧值 + 新值创建数组被存储。

标签: google-chrome-extension google-chrome-storage


【解决方案1】:

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

以下示例代码应该允许您将newArrEntry 添加到存储在chrome.storage.sync 中的现有array

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-09-04
    • 1970-01-01
    • 2013-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-24
    相关资源
    最近更新 更多