我正在尝试将用户输入的数据存储在popup.html
. 在数据上使用 jQuerywindow unload
应该同步并且window ready
数据应该被恢复。但是,当打开popup.html
textarea 的内容时是undefined
. 这是我正在加载的 jQuery 代码popup.html
:
$(window).unload (
function save() {
var textarea = document.querySelector("#contacts").value;
// Old method of storing data locally
//localStorage["contacts"] = textarea.value;
// Save data using the Chrome extension storage API.
chrome.storage.sync.set({contacts: textarea}, function() {
console.log("Contacts saved");
});
});
$(window).ready(
function restore() {
var textarea = document.querySelector("#contacts");
// Old method of retrieving data locally
// var content = localStorage["contacts"];
chrome.storage.sync.get('contacts', function(r) {
console.log("Contacts retrieved");
var content = r["contacts"];
textarea.value = content;
});
});