如何使我的Chrome扩展程序记住拨动开关或其他操作?

时间:2019-03-19 08:44:28

标签: 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>

我的清单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
}

1 个答案:

答案 0 :(得分:1)

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

尽管还有其他方法,但使用同步API会记住设备之间的用户设置。