在 chrome.storage 中创建数组并检索数据

Ham*_*bad 3 javascript arrays google-chrome-extension

所以我现在有点进退两难。我似乎无法在 chrome.storage 中添加一个数组,然后再检索它。这是我现在拥有的代码:

function() {
    chrome.storage.sync.get({ObjectName: []}, function (result) {
    var ObjectName = result.ObjectName;
    ObjectName.push({ArrayName: document.getElementById("field")});
    });
Run Code Online (Sandbox Code Playgroud)

现在检索它并显示它:

chrome.storage.sync.get({ArrayName: function(value) {
            for(i=0; i<value.length; i++) { 
                document.write(value)
            };
Run Code Online (Sandbox Code Playgroud)

我得到的错误可能像语法问题一样简单,相当于:

错误:调用表单 get(object) 与定义 get 不匹配(可选字符串或数组或对象键,函数回调)

Mad*_*han 5

您必须使用 set 方法将值设置为 chrome.storage

这是一个如何做到的例子

使用 set 将数组存储到 chrome 存储

var testArray=["test", "teste", "testes"];

chrome.storage.sync.set({
    list:testArray
}, function() {
    console.log("added to list");
});
Run Code Online (Sandbox Code Playgroud)

通过调用 updatemethod 使用 get 和 modify if 获取 arrayValue

chrome.storage.sync.get({
    list:[]; //put defaultvalues if any
},
function(data) {
   console.log(data.list);
   update(data.list); //storing the storage value in a variable and passing to update function
}
);  

function update(array)
   {
    array.push("testAdd");
    //then call the set to update with modified value
    chrome.storage.sync.set({
        list:array
    }, function() {
        console.log("added to list with new values");
    });
    }
Run Code Online (Sandbox Code Playgroud)