微信读书自动深色模式

根据系统设置自动切换深色模式,深色用的是官方的样式

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         微信读书自动深色模式
// @version      1.0.1
// @description  根据系统设置自动切换深色模式,深色用的是官方的样式
// @namespace    https://weread.qq.com/
// @match        https://weread.qq.com/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=weread.qq.com
// @author       bowencool
// @license      MIT
// @homepageURL  https://greasyfork.org/scripts/477034
// @supportURL   https://github.com/bowencool/Tampermonkey-Scripts/issues
// @grant        none
// ==/UserScript==

(function () {
  "use strict";
  async function toggle(
    isDarkMode = window.matchMedia("(prefers-color-scheme: dark)").matches
  ) {
    const currentThemeIsWhite =
      document.body.classList.contains("wr_whiteTheme");
    console.log({ isDarkMode, currentThemeIsWhite });
    if (isDarkMode) {
      if (currentThemeIsWhite) {
        document.cookie = "wr_theme=dark; path=/; domain=.weread.qq.com;";
        if (location.pathname.startsWith("/web/reader")) {
          // const button = await waitForElementToExist(
          //   ".readerControls_item.dark"
          // );
          // button.click();
          location.reload();
        } else {
          document.body.classList.remove("wr_whiteTheme");
        }
      }
    } else {
      if (!currentThemeIsWhite) {
        document.cookie = "wr_theme=white; path=/; domain=.weread.qq.com;";
        if (location.pathname.startsWith("/web/reader")) {
          location.reload();
        } else {
          document.body.classList.add("wr_whiteTheme");
        }
      }
    }
  }
  toggle();
  window
    .matchMedia("(prefers-color-scheme: dark)")
    .addEventListener("change", (e) => {
      toggle(e.matches);
    });
})();