Created
July 29, 2024 13:58
-
-
Save denblackstache/2016b5824dc49f11b841468bed354c82 to your computer and use it in GitHub Desktop.
Reddit System Theme Switch UserScript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name Reddit System Theme Switch | |
// @namespace http://tampermonkey.net/ | |
// @version 1.0 | |
// @description Auto switch Reddit light/dark mode based on system theme. Based on new Reddit UI. | |
// @author Denis Semenenko <[email protected]> | |
// @match https://*.reddit.com/* | |
// @grant none | |
// ==/UserScript== | |
(function () { | |
'use strict'; | |
function syncTheme() { | |
const modeSwitch = document.querySelector('[name=darkmode-switch-name]'); | |
if (!modeSwitch) { | |
return console.warn(`Dark mode switch not found on reddit.com, skipping auto dark mode.`); | |
} | |
const isLightToggled = modeSwitch.getAttribute('checked') === null; | |
const lightMode = window.matchMedia('(prefers-color-scheme: light)').matches; | |
if (isLightToggled !== lightMode) { | |
modeSwitch.click(); | |
} | |
} | |
window.addEventListener('load', () => { | |
syncTheme(); | |
window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', syncTheme); | |
}); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment