在我的内容脚本中,我设置了几个chrome.storage
值,在我的弹出脚本中,我读取并获取这些值。
我已经尝试了所有方法,但它不断返回"未定义",这没有意义,因为该值肯定不是未定义的。
我的内容.js脚本:
imgs=["test.png","othertest.png"]
for (i = 0; i < imgs.length; i++){
var key = "image"+i
var value = imgs[i]
chrome.storage.sync.set({key: value}, function() {
console.log('Value is set to ' + value + ' and key is set to '+key);
})
}
chrome.storage.sync.set({"count": imgs.length}, function() {
console.log('Saved count value. Current count: '+imgs.length)
});
我的弹出窗口.js脚本:
document.addEventListener('DOMContentLoaded',function(){
chrome.storage.sync.get(['count'], function(result) {
console.log(result.value)
for (i = 0; i < result.value; i++){
chrome.storage.sync.get(['image'+i], function(result) {
var img = document.createElement("img")
img.src = result.value
document.body.appendChild(img)
})
}
})
})
我的弹出窗口.html文件:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>
<body>
<h1>Images</h1>
<script src="popup.js"></script>
</body>
</html>
popup.js
脚本应该从chrome.storage获取每个图像,并将它们显示在弹出窗口中。任何帮助将不胜感激。
这要归功于我找到了这个答案!
弹出窗口.js:
document.addEventListener('DOMContentLoaded',function(){
function getCountValue(callback) {
chrome.storage.sync.get("count", callback);
}
function getImageValue(number,callback) {
chrome.storage.sync.get(['image'+number], callback);
}
getCountValue(function (count) {
for (i = 0; i < count.count; i++){
getImageValue(i, function(imgvalue){
for (var property in imgvalue) {
if (imgvalue.hasOwnProperty(property)) {
var img = document.createElement("img")
img.src = imgvalue[property]
document.body.appendChild(img)
}
}
})
}
})
})