Nyaa 自動黑暗模式

基於瀏覽器的佈景主題設定,自動從明亮和黑暗模式間切換。

目前為 2023-02-15 提交的版本,檢視 最新版本

// ==UserScript==
// @name               Auto Dark Mode for Nyaa
// @name:zh-TW         Nyaa 自動黑暗模式
// @description        Automatically switch the theme between light and dark, based on the browser's color scheme preference.
// @description:zh-TW  基於瀏覽器的佈景主題設定,自動從明亮和黑暗模式間切換。
// @icon               https://icons.duckduckgo.com/ip3/nyaa.si.ico
// @author             Jason Kwok
// @namespace          https://jasonhk.dev/
// @version            1.1.2
// @license            MIT
// @match              https://nyaa.si/*
// @match              https://sukebei.nyaa.si/*
// @run-at             document-idle
// @grant              none
// @supportURL         https://greasyfork.org/scripts/448484/feedback
// ==/UserScript==

/* global setThemeLight, setThemeDark */

const query = window.matchMedia("(prefers-color-scheme: dark)");

query.addEventListener("change", updateTheme);
updateTheme(query);

function isDarkTheme()
{
    return document.body.classList.contains("dark");
}

function updateTheme({ matches: isDarkMode })
{
    if (isDarkTheme() !== isDarkMode)
    {
        (isDarkMode ? setThemeDark() :setThemeLight());
    }
}