无法使用JavaScript保存textarea中的值
我试图使用存储API保存扩展html文件中的文本区域中的信息,但由于某些原因,文本区域中的数据无法保存。 如果在文本框中键入并单击“保存”,它将显示“已保存”,但如果重新打开“选项”菜单,它将为空。有人能帮助确定这个问题吗 JavaScript文件:无法使用JavaScript保存textarea中的值,javascript,google-chrome-extension,Javascript,Google Chrome Extension,我试图使用存储API保存扩展html文件中的文本区域中的信息,但由于某些原因,文本区域中的数据无法保存。 如果在文本框中键入并单击“保存”,它将显示“已保存”,但如果重新打开“选项”菜单,它将为空。有人能帮助确定这个问题吗 JavaScript文件: // Saves options to chrome.storage.sync function save_options() { var websites = document.getElementById('websites').value
// Saves options to chrome.storage.sync
function save_options() {
var websites = document.getElementById('websites').value;
chrome.storage.sync.set({'websites': websites}, function() {
// Update status to let user know options were saved.
var status = document.getElementById('status');
status.textContent = 'Options saved.';
setTimeout(function() {
status.textContent = '';
}, 750);
});
}
// stored in chrome.storage.sync
function restore_options() {
chrome.storage.sync.get('websites', function(items) {
document.getElemntById('websites').value = items.websites;
});
}
document.addEventListener('DOMContentLoaded', restore_options);
document.getElementById('save').addEventListener('click', save_options);
HTML文件:
<!DOCTYPE html>
<html>
<head>
<title>Panic Button Options</title>
<style>
body: { padding: 10px; }
</style>
</head>
<body>
<textarea id="websites" cols="112" rows="10"></textarea>
<div id="status"></div>
<button id="save">Save</button>
<script src="options.js"></script>
</body>
</html>
紧急按钮选项
正文:{padding:10px;}
拯救
我觉得你的代码没问题。你也可以添加页面的HTML吗?另外,尝试通过在回调中检查chrome.runtime.lastError
进行调试。我已经添加了HTML代码,最终,在我获得要保存的愚蠢文本区域后,我会向其添加更多选项。还原代码中的一些输入错误,例如getElemntById
?啊,我看到了输入错误,由于某种原因,我在重读代码时跳过了这个步骤。