我必须点击按钮"保存"如果我想接受我的Chrome扩展程序的任何选项
如何在没有"保存"的情况下保存扩展程序的设置按钮? (在检查任何标签或选择任何选项后立即)
options.html
<html>
<head>
<title>test</title>
</head>
<body>
What to do?:
<select id="whattodo">
<option value="first">first action</option>
<option value="two">second action</option>
</select>
<br><br>
<label>
<input type="checkbox" id="testf">Test function
</label>
<br><br>
<button id="save">Save</button>
<div id="status"></div>
</body>
<script src="options.js"></script>
</html>
options.js
function save_options() {
var select1 = document.getElementById("whattodo");
var whattodo = select1.children[select1.selectedIndex].value;
chrome.storage.local.set({'whattodo': whattodo});
if (document.getElementById('testf').checked) {
localStorage.setItem('testf', "true");
} else {
localStorage.setItem('testf', "false");
}
var status = document.getElementById("status");
status.innerHTML = "Options saved";
setTimeout(function() {
status.innerHTML = "";
}, 750);
}
function restore_options() {
if (localStorage.getItem('testf') == "true") {
document.getElementById("testf").checked=true;
} else {document.getElementById("testf").checked=false;}
chrome.storage.local.get('whattodo', function(result) {
var whattodo = result.whattodo;
for (var i = 0; i < document.getElementById("whattodo").children.length; i++) {
var child = document.getElementById("whattodo").children[i];
if (child.value == whattodo) {
child.selected = "true";
break;
}
}
});
}
document.addEventListener('DOMContentLoaded', restore_options);
document.querySelector('#save').addEventListener('click', save_options
);