由于某种原因,我必须使用保存的oldValue
更新本地存储这是我的代码
//for test
function getJsonLength(jsonData) {
var jsonLength = 0;
for (var item in jsonData) {
jsonLength++;
}
return jsonLength;
}
// main function
function updateData(key, newData) {
// console.log(key);
// console.log('new data length: ' + getJsonLength(newData)); // got 60
chrome.storage.local.get(key, function (oldData) {
// console.log('old data length: ' +
getJsonLength(oldData[key])); // got 60
// console.log(res[key]);
$.each(newData, function (name, value) {
oldData[key][name] = value;
});
chrome.storage.local.set({ key: oldData[key] }, function () {
// console.log('final data length: ' + getJsonLength(oldData[key])); // right & got 120
// console.log(oldData[key]); // right & get final new data
});
});
}
我设置了manifest.json:"permissions": ["storage", "unlimitedStorage"]
并重新加载扩展程序。
每一步都是正确的,但存储的值是错误的。
我不知道哪里出了问题,请帮帮我:(