简体   繁体   English

如何保留 Firefox 附加弹出窗口的复选框 state?

[英]How do I keep the checkbox state for Firefox add-on popup?

I'm trying to make a toggle switch based on a checkbox.我正在尝试基于复选框进行切换。 The checkbox should only change when the user toggles the switch.该复选框仅应在用户切换开关时更改。

Here's my JavaScript code:这是我的 JavaScript 代码:

var toggle = false;
document.getElementById("toggle").onclick = function () {
  if (this.checked == true) {
    toggle = true;
    document.getElementById("toggleText").textContent = "ENABLED";
    browser.browserAction.setIcon({ path: "/icons/icon.png" });
  } else {
    document.getElementById("toggleText").textContent = "DISABLED";
    browser.browserAction.setIcon({ path: "/icons/icon-inactive.png" });
  }
};

However, the checkbox state is always set to unchecked when I reopen the popup.但是,当我重新打开弹出窗口时,复选框 state 始终设置为未选中。 Any ideas about how I can achieve making this?关于如何实现这一目标的任何想法?

Save the state to local storage.将 state 保存到本地存储。 Here's a simplified example:这是一个简化的示例:

manifest.json: manifest.json:

{
    "manifest_version": 2,
    "name": "Save states",
    "version": "1.0.0",
    
    "browser_action": {        
        "default_title": "States",
        "default_popup": "index.html"
    }
}

index.html:索引.html:

<html>
    <head>
    </head>
    <input type="checkbox" id="checkbox"> checkbox
    <script src="index.js"></script>
</html>

index.js: 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));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将Greasemonkey脚本发布为Firefox附加组件? - How do I publish a Greasemonkey script as a Firefox add-on? 如何在Firefox扩展程序(附加组件)中要求文件? - How do I require files in Firefox extension (add-on)? 如何避免在我的Firefox插件中出现“TypeError:无法访问死对象”的状态? - How can I avoid state of “TypeError: can't access dead object” in my Firefox add-on? Firefox附加组件仅在调试状态下执行 - Firefox add-on execute only in debugging state Firefox附加SDK侧边栏不兼容-如何警告用户? - Firefox add-on sdk sidebar incompatibility - How do I warn the user? 如何在私有模式下编写自动关闭的Firefox附加组件? - How Do I Write a Firefox Add-On That Turns Itself Off When in Private Mode? 如何从firefox插件获取浏览器的窗口高度? - How do I get browser's window height from firefox add-on? 在Firefox重新启动加载项中,如何在打开新窗口(监听窗口打开)时运行代码? - In a Firefox restartless add-on, how do I run code when a new window opens (listen for window open)? 如何访问Firefox附加SDK的onHttpRequest函数中提供的数据? - How do I access the data given in the onHttpRequest function in the Firefox Add-on SDK? 如何使用Firefox附加组件中的jQuery和Javascript? - How can I use jQuery and Javascript from firefox add-on?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM