0
我想做一个扩展,需要保存一些文本(URL)。数据的设置看起来不错,但试图检索数据是问题。chrome.storage.local.get没有获取数据
扩展清单在内容脚本区域中同时具有弹出式JavaScript和内容JavaScript。
{
"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"]
}
]
}
的网址会存储在浏览器本地存储区:
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函数。但是我无法找到在content.js
//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