Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save denblackstache/2016b5824dc49f11b841468bed354c82 to your computer and use it in GitHub Desktop.
Save denblackstache/2016b5824dc49f11b841468bed354c82 to your computer and use it in GitHub Desktop.
Reddit System Theme Switch UserScript
// ==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