ResetEra Auto Dark Mode

Automatically toggle built-in dark mode on resetera.com

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

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

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

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

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

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name         ResetEra Auto Dark Mode
// @namespace    http://tampermonkey.net/
// @version      0.2
// @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 => {
        const light_dark_switch = document.querySelector('label#js-XFUniqueId3.thstyleswitch_toggleSwitch > input.thstyleswitch_toggleSwitch__checkbox');
        if (!Boolean(light_dark_switch)) {
            console.log('site updated, current script no longer works');
            return;
        }
        let is_light = !(light_dark_switch.checked);
        if (on === is_light) light_dark_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);
    }
})();