Skip to content

Instantly share code, notes, and snippets.

View sampotts's full-sized avatar

Sam Potts sampotts

View GitHub Profile
@sampotts
sampotts / reddit-dark-mode-system.js
Last active June 1, 2023 00:27
A user script to toggle Reddit's dark mode automatically based on your system preferences
// Get the initial state
const query = window.matchMedia('(prefers-color-scheme: dark)');
function toggleDarkMode(toggle = query.matches) {
// Open the menu
document.querySelector('#USER_DROPDOWN_ID[aria-expanded="false"]')?.click();
// Get the wrapper item
const wrapper = Array.from(document.querySelectorAll('button')).find(({ innerText }) => innerText.toLowerCase() === 'dark mode');
const checkbox = wrapper?.querySelector(`[aria-checked="${!toggle}"]`);