在chrome.storage中创建数组并检索数据 [英] Making an Array in chrome.storage and retrieving data

查看:171
本文介绍了在chrome.storage中创建数组并检索数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

现在检索并显示它:

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

我得到的错误,可能是如语法问题一样简单,等于:



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

解决方案

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

以下是一个如何做到这一点的例子:

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

  var testArray = [test,teste,testes]; 

chrome.storage.sync.set({
list :testArray
},function(){
console.log(added to list);
});

获得a rrayValue使用get和modify如果调用updatemethod

  chrome.storage.sync.get({
list:[]如果有任何

函数(数据){
console.log(data.list);
update(data.list;)//将存储值存储在变量中并传递给更新函数
}
);

function update(array)
{
array.push(testAdd);
//然后调用set来更新修改后的值
chrome.storage.sync.set({
list:array
},function(){
console .log(用新值添加到列表中);
});
}


So I'm in a bit of a dillema right now. I can't seem to be able to add an Array in chrome.storage and later retrieving it. Here's the code I have right now:

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

Now to retrieve it and display it:

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

The error I am getting, which might be as simple as a syntax issue, amounts to:

Error: Invocation of form get(object) doesn't match definition get(optional string or array or object keys, function callback)

解决方案

You have to use set method to set values to chrome.storage

Here's an example of how to do it

To store an array to chrome storage using set

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

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

To get the arrayValue using get and modify if by calling updatemethod

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

这篇关于在chrome.storage中创建数组并检索数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆