知乎自動暗色模式

讓知乎的主題與系統主題保持同步。

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         ZhihuThemeSync
// @name:zh-CN 知乎自动深色模式
// @name:zh-TW 知乎自動暗色模式
// @namespace    http://tampermonkey.net/
// @version      2025-10-14 .3
// @description  Keeps Zhihu’s theme in sync with your OS theme.
// @description:zh-CN 让知乎的主题与系统主题保持同步。
// @description:zh-TW 讓知乎的主題與系統主題保持同步。
// @author       Ethan Dong
// @match        https://*.zhihu.com/*
// @icon         https://www.google.com/s2/favicons?sz=256&domain=https://www.zhihu.com/
// @grant        none
// @run-at       document-start
// @license MIT
// ==/UserScript==

(function() {

    const themeMedia = window.matchMedia('(prefers-color-scheme: dark)');
    function updateThemeStatus(e){
        console.log("Triggered")
        let url = new URL(window.location.href);
        let params = url.searchParams;
        let sysTheme = "";
        let CurrentPageTheme = "";

        if (params.has("theme")){
            CurrentPageTheme = params.get("theme")
        }else{
            sysTheme = null
        }

        if (e.matches){
            sysTheme = "dark"
            params.set("theme","dark")

        }else{
            sysTheme = "light"
            params.set("theme","light")

        }

        if (CurrentPageTheme = null || sysTheme != CurrentPageTheme){
            window.location.href = url.toString();
            console.log("reloaded")
        }

    }
    updateThemeStatus(themeMedia);
    themeMedia.addEventListener('change', updateThemeStatus);


}
)();