【发布时间】:2021-07-03 00:13:07
【问题描述】:
我正在开发 Google 扩展程序并使用 Google 存储。
以下是我保存和访问已保存数据的代码。我检查了控制台,数据保存正确。但是,当我使用 chrome.storage.local.get 访问此数据时,控制台返回未定义的值而不是保存的值。
保存代码:popup.html 的 save.js
function save() {
# the id is for a textarea in another html page
var text = document.getElementById("text").value;
chrome.storage.sync.set({"txt": text}, function(){
# console states the correct value
console.log('Value is set to ' + text);
});
# this is the page I want to retrieve the saved data in
window.location.replace("anotherHTMLpage.html");
}
访问代码:retrieve.js for anotherHTMLpage.html
function retrieve() {
chrome.storage.sync.get(["txt"], function(data) {
# console says "value currently is undefined"
console.log('Value currently is ' + data.key);
});
}
提前感谢您的帮助!
【问题讨论】:
标签: google-chrome-extension undefined google-chrome-storage