一部のテキスト(URL)の保存が必要な拡張機能を作成しようとしています。データの設定は問題ないようですが、データを取得しようとするのが問題です。chrome.storage.local.getデータが取得されない
拡張マニフェストには、ポップアップJavaScriptとコンテンツスクリプト領域のコンテンツJavaScriptがあります。
var b = document.getElementById("l"); //l is a button
b.onmouseup = function() {
var r = prompt("Please enter the website to add.");
var g = [];
chrome.storage.local.get("whitelist", function (i) {
if (i['whitelist'] !== undefined) {
var g = i['whitelist'];
}
});
g.push(r);
chrome.storage.local.set({
"whitelist": g
}, function() {
console.log("done")
});
}
これが動作しているようです、と私は手動でのデータのためのget関数を入力することができます
{
"manifest_version": 2,
"name": "Extension_Name",
"version": "0.0.1",
"browser_action": {
"default_title": "Extension_title",
"default_icon": "icon.png",
"default_popup": "popup.html"
},
"permissions": ["storage"],
"content_scripts": [
{
"matches": [
"<all_urls>"
],
"js": ["content.js","popup.js"]
}
]
}
URLはクロームローカル記憶領域に記憶されています。 2番目のスニペットでは
、var g = i['whitelist']
が新しい、より狭いスコープで宣言されています。しかし、私は、私はいくつかの可能性のある問題を参照してください
//getting website whitelist
d = null;
var inw = false;
chrome.storage.local.get("whitelist", function (p) {
d = p['whitelist'];
}); //why doesnt this work???
console.log(d); //testing (returns null in the console...)
for (var j in d) { //this script doesnt run because d is not set to an array
alert(window.location.host.replace(/\./g, ""));
if (d[j].replace(/\./g, "").replace(/:\/\//g, ".")
.split(".")[1] === window.location.host.replace(/\./g, "")) {
inw = true;
alert("true");
}
}
関連:[非同期呼び出しからの応答を返すにはどうすればいいですか](https://stackoverflow.com/q/14220321) – Makyen