私はchrome.storage.local
にデータを保存するChrome拡張機能を作成しています。ただし、最後のレコードデータのみを保存します。私は何が間違っているのか説明できますか?なぜchrome.storage.localは最後のレコードのみを保存しますか?
Chromeブラウザで設定をリセットした後、localStorageが空であるため、通常のlocalStorageは不良です。
chrome.runtime.onMessage.addListener(function (request) {
if (request.type === 'setInLocalStorage') {
var key = request.key;
//alert(key);
chrome.storage.local.set({key: request.data},function() {
alert('data was saved');
});
}
return true;
});
chrome.runtime.onMessage.addListener(function (request, sender, SendResponse) {
if (request.type === 'getFromStorage') {
chrome.storage.local.get(null, function (items) {
var allKeys = Object.keys(items);
if (allKeys.length != 0) {
for (var i = 0; i < allKeys.length; i++) {
alert(items[allKeys[i]]);
if (items[allKeys[i]] === request.value) {
SendResponse(true);
} else if (i === allKeys.length - 1) {
SendResponse(false);
}
}
} else {
SendResponse(false);
}
});
}
return true;
});
なぜ'get()'と 'set()' 'storage.local'にメッセージを送りますか? 'storage.local'は、バックグラウンドコンテキストで実行されているコンテンツスクリプトとスクリプトの両方から利用できます。情報を伝える必要はありません。 – Makyen