Greasy Fork

Greasy Fork is available in English.

ResetEra Auto Dark Mode

Automatically toggle built-in dark mode on resetera.com

当前为 2020-10-23 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         ResetEra Auto Dark Mode
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  Automatically toggle built-in dark mode on resetera.com
// @author       Nathaniel Wu
// @match        *.resetera.com/*
// @license      Apache-2.0
// @supportURL   https://gist.github.com/Nathaniel-Wu/13f3c865e190c2b182e41b9978c49782
// @grant        none
// ==/UserScript==

(function () {
    'use strict';
    const setDarkMode = on => {
        let switches = document.querySelectorAll('div[data-content="navigation"] a[data-xf-click="thstyleswitch"]');
        let dark_switch = null, light_switch = null, is_light;
        switches.forEach(e => {
            let innerText = e.innerText.toLowerCase();
            if (Boolean(innerText.match(/light/)))
                light_switch = e;
            else if (Boolean(innerText.match(/dark/)))
                dark_switch = e;
        });
        if (!(Boolean(dark_switch) && Boolean(light_switch))) {
            console.log('site updated, current script no longer works');
            return;
        }
        let light_switch_parent = light_switch.closest('li.re_styleSwitch');
        if (!((is_light = light_switch_parent.classList.contains('re_styleSwitch--hide')) || light_switch_parent.classList.contains('re_styleSwitch--show')))
            is_light = Boolean(light_switch_parent.getAttribute('style').match(/display:\s*none/));
        if (on && is_light)
            dark_switch.click();
        else if (!on && !is_light)
            light_switch.click();
    }
    if (window.matchMedia) {// if the browser/os supports system-level color scheme
        setDarkMode(window.matchMedia('(prefers-color-scheme: dark)').matches);
        window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', e => setDarkMode(e.matches));
    } else {// otherwise use local time to decide
        let hour = (new Date()).getHours();
        setDarkMode(hour > 18 || hour < 8);
    }
})();