javascript - 如何让我的 chrome 扩展程序记住拨动开关或其他操作?

标签 javascript html google-chrome-extension

我的 chrome 扩展不记得拨动开关。我正在尝试进行扩展以在切换开关打开时阻止 youtube 提要和评论。我尝试在评论部分这样做,但它不起作用。

if(document.getElementById("input1").checked == true){
  document.getElementByClassName("ytd-comments").style.display = "none";
}
<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
  </head>
  <body>
    <!-- Rounded switch -->
    <div>
    <label class="switch">
    <input id="input0" type="checkbox">
    <span class="slider round"></span>
    </label>
    <p class="ch-text">yotube feed<p>
      </div>
    <!-- switch for the comments -->
    <div>
    <label class="switch">
    <input id= "input1" type="checkbox">
    <span class="slider round"></span>
    </label>
    <p class="ch-text">comment section<p>
      </div>
    <link rel="stylesheet" href="style.css">
    <script src="background.js"></script>
  </body>
</html>

我的 list json:

  {
  "name": "Youtube feed Blocker",
  "description": "Removing the feed and comments from the youtube to stop wasting time",
  "icons": {"128": "icon.png"},
  "version": "1.0",
  "permissions": [
      "tabs", "http://*.youtube.com/*", "https://*.youtube.com/*"
  ],
  "browser_action": {
      "default_title": "Removed the recommended videos sections",
      "default_popup": "popup.html",
      "default_icon": "icon.png",
      "persistent": true
  },
    "content_scripts": [{
        "css": ["style.css"],
        "matches": ["*://www.youtube.com/", "*://www.youtube.com/watch*", "*://www.youtube.com/*", "*://www.youtube.com/user*"],
        "all_frames": true,
        "run_at": "document_start"
    }],

  "manifest_version": 2
}

最佳答案

您可以使用 storage.sync API ,如此处解释:https://developer.chrome.com/extensions/options

虽然还有其他方法,但使用同步 API 会跨设备记住用户的设置。

关于javascript - 如何让我的 chrome 扩展程序记住拨动开关或其他操作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55236700/

相关文章:

javascript - 获取 Chrome pageAction 图标以显示 JSON 格式的 URL 列表

javascript - JQuery JSON 解析/AJAX 问题 ('remove' 和 '__proto__' 数组中的键?)

javascript - 如果元素尚未显示,html 会转到另一个页面的特定元素吗?

javascript - 清除不活动时的表单字段

javascript - 在 DNN 中,如何将我的 .js 文件或 JS 代码与新创建的页面结合起来

javascript - 所有选项卡 chrome 扩展的屏幕截图

javascript - 函数在传递给另一个函数后成为对象

jquery - 页面顶部的子菜单淡入和末尾的淡出

jquery - 显示 2 个 div 彼此重叠,而其中一个 div 依赖于其他 div

javascript - 在内容脚本之前注入(inject) javascript 变量