chrome.storage.local
Google Chrome 拡張機能では、 (localStorage ではなく)使用したい理由は次のとおりです。
- キーと値のペアを使用すると、値は (文字列のみではなく) 任意のオブジェクトにすることができます。
- セッターを使用してデータ モデルを変更すると
storage.set
、イベント リスナーをトリガーできます
を使用してstorage.set
、どのように変数のキー名を取得できますか?
注: セッターを使用しない場合は可能ですstorage[v1]
が、オブジェクトを変更してもイベント リスナーはトリガーされません。
var storage = chrome.storage.local;
var v1 = 'k1';
storage.set({v1:'s1'});
storage.get(v1,function(result){
console.log(v1,result);
//console output = k1 {}
});
storage.get('v1',function(result){
console.log(result);
//console output = {v1:'s1'}
});