ここでクロムエクステンションで最初に試行します。銀行のウェブサイトの取引リストに永続的なチェックボックスを追加して、外部の予算ツールとの調整を支援したいと思います。すべて機能していますが、クロムストレージAPI(ローカルを使用)からデータを取得することはできません。結果は、ストレージに正常に書き込まれた同じコールバックで値を取得することをテストしていても、常に定義されていません。chrome.storage.localは常に未定義を返します
manifest.jsonを:
{
"name": "Reconciler",
"version": "1.0.0",
"manifest_version": 2,
"background": {
"scripts": ["injector.js"],
"persistent": false
},
"permissions": [
"activeTab",
"storage",
"https://mybank*"
],
"browser_action": {
"default_title": "Add reconciling buttons"
}
}
injector.js:
chrome.browserAction.onClicked.addListener(function(tab) {
chrome.tabs.insertCSS(null, {file: "reconciler.css"});
chrome.tabs.executeScript(null, {file: "jquery-3.2.0.min.js"});
chrome.tabs.executeScript(null, {file: "md5.js"});
chrome.tabs.executeScript(null, {file: "reconciler.js"});
});
reconciler.js:
$("#transactions thead tr").append("<th class='reconcile'>Reconcile</th>");
$("#transactions tbody tr").each(function(){
$this = $(this);
var rel_hash = "md5_" + hex_md5($this.find("img.expand-trans").first().attr("rel"));
$this.append("<td class='reconcile'><input type='checkbox' id='" + rel_hash + "' name='" + rel_hash + "'></td>");
chrome.storage.local.get(rel_hash, function(items) {
$("#"+rel_hash).attr("checked", items[rel_hash]);
});
});
$("#transactions input[type=checkbox]").change(function(){
var rel_hash = $(this).attr("id");
var checked = this.checked;
chrome.storage.local.set({rel_hash: checked}, function() {
if(chrome.runtime.lastError) {
console.error(
"Error setting " + key + " to " + JSON.stringify(data) +
": " + chrome.runtime.lastError.message
);
} else {
console.log('Saved ' + rel_hash + '=' + checked);
chrome.storage.local.get(rel_hash, function(items) {
console.log(rel_hash + "=" + items[rel_hash]);
});
}
});
});
コンソール出力をチェックし、チェックをはずす:
Saved md5_516654acf57d9bd95cdbe497f7ca6c8d=true
md5_516654acf57d9bd95cdbe497f7ca6c8d=undefined
Saved md5_516654acf57d9bd95cdbe497f7ca6c8d=false
md5_516654acf57d9bd95cdbe497f7ca6c8d=undefined
Saved md5_ee541d5b1d95768cef9c257ca88c8ced=true
md5_ee541d5b1d95768cef9c257ca88c8ced=undefined
すべては問題ありません。私はいつも[ローカルエリア]または[同期]スタージを見るために[ストレージエリアエクスプローラ](https://chrome.google.com/webstore/detail/storage-area-explorer/ocfjjjjhkpapocigimmppepjgfdecjkb)を使用します。また、 'items'変数や' Object.keys(items) 'のログをコンソール化することもできます。 – Ionut
Bah、Storage Area Explorerはキーにrel_hashの値を使用する代わりに{rel_hash":true}を保存しています。 – shadfc