How do I keep the checkbox state for Firefox add-on popup?
Save the state to local storage. Here's a simplified example:
manifest.json:
{
"manifest_version": 2,
"name": "Save states",
"version": "1.0.0",
"browser_action": {
"default_title": "States",
"default_popup": "index.html"
}
}
index.html:
<html>
<head>
</head>
<input type="checkbox" id="checkbox"> checkbox
<script src="index.js"></script>
</html>
index.js:
const checkboxStorageState = JSON.parse(localStorage.getItem("checkboxStorageState"));
const check = document.getElementById("checkbox");
let currentState = false;
if(checkboxStorageState) {
currentState = checkboxStorageState;
check.checked = currentState;
}
check.onclick = function() {
if(this.checked) {
currentState = true;
}
else {
currentState = false;
}
localStorage.setItem("checkboxStorageState", JSON.stringify(currentState));
}