chrome.storage.local.通过OBJ键获取一个对象并迭代



我使用chrome.storage.local.get功能遇到了麻烦。我都有工作,除了我不了解如何处理.get功能。我想通过单击一个按钮来复制一个从一个镀铬标签到另一个对象。我有一个循环以获取对象键,并且可以通过以下方式保存在Chrome Local Storage中的成功。

chrome.storage.local.set({
    'copiedObj': obj
});

但是,当尝试使用.get函数时,我无法指定我想要的键 number 。我需要密钥编号,因为键及其值每次都会改变,因为我正在复制不同页面的数据。

$('#targetInfo').click(function(){
   console.log('context 2 received');
    var storage = chrome.storage.local.get('copiedObj', function (items) {
    console.log(items); 
    });
});

这使我获得了完整的对象键和值,但是当试图更改它以获取第一个密钥时(将脚本内部的console.log更改为项目[0]时,我在控制台中收到一个未定义的错误消息。<<<<<<<<<<<<<</p>

我希望它通过所有键进行迭代,并将键分配给变量,并为变量分配并执行函数。对于每个键的时间。我在代码中写了一个详细的说明:

//Get the DOM input field
var specific_name = document.getElementById('new_specific_name');
var specific_value = document.getElementById('new_specific_value');
//Start loop, from 0 to the last object keys
for (i=0;i<items.length;i++) {
    //Set specific_name value to the first key inside the object
    specific_name.value = XXX[i];
    //Set specific_value value to the matching key value inside the object
    specific_value.value = ZZZ[i];
    //Execute this function
    add_new_specific();
}

(最小解决方案,等待澄清(,如前所述,

var storage = chrome.storage.local.get('copiedObj', function (items) {
    console.log(items); 
});

应该是

var storage = chrome.storage.local.get('copiedObj', function (result) {
    console.log(result.items);
    console.log(result.items[0]); // if items is an array...
});

要通过对象循环,请参见https://stackoverflow.com/a/18202926/1587329。您需要在 .get中的匿名函数。

中进行全部

最新更新