Auto Dark Mode for Reddit

Automatically switches the theme based on your OS dark mode preference

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey, Greasemonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

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

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

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

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्क्रिप्ट व्यवस्थापक एक्स्टेंशन इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्क्रिप्ट व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्टाईल व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

// ==UserScript==
// @name         Auto Dark Mode for Reddit
// @namespace    http://tampermonkey.net/
// @version      0.3
// @description  Automatically switches the theme based on your OS dark mode preference
// @author       Avi (https://avi12.com)
// @copyright    2025 Avi (https://avi12.com)
// @license      MIT
// @match        https://www.reddit.com/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=reddit.com
// @grant        none
// ==/UserScript==

(function () {
  "use strict";

  /**
   * @returns {HTMLButtonElement}
   */
  const getElButtonProfile = () => document.querySelector("button#expand-user-drawer-button");

  // noinspection CssInvalidHtmlTagReference
  /**
   * @returns {HTMLElement}
   */
  const getElDarkToggle = () => document.querySelector("#darkmode-list-item faceplate-switch-input");

  const OBSERVER_OPTIONS = { childList: true, subtree: true };

  function openProfileDrawerAndToggleTheme() {
    const { activeElement } = document;
    const elButtonProfile = getElButtonProfile();
    new MutationObserver((_, observer) => {
      const elDarkToggle = getElDarkToggle();
      if (!elDarkToggle) {
        return;
      }
      const { shadowRoot } = elDarkToggle;
      if (!shadowRoot) {
        return;
      }
      const elDarkToggleInput = shadowRoot.querySelector("input");
      if (!elDarkToggleInput) {
        return;
      }
      observer.disconnect();
      requestAnimationFrame(() => {
        elDarkToggle.click();
        elButtonProfile.click();
        activeElement.focus();
      });
    }).observe(document, OBSERVER_OPTIONS);
    elButtonProfile.click();
  }

  const darkQuery = matchMedia("(prefers-color-scheme: dark)");
  new MutationObserver((_, observer) => {
    if (!getElButtonProfile()) {
      return;
    }

    const themeNew = darkQuery.matches ? "dark" : "light";
    const themeCurrent = document.cookie.match(/theme=([12])/)[1] === "1" ? "light" : "dark";
    const isChangeTheme = themeNew !== themeCurrent;
    observer.disconnect();
    if (!isChangeTheme) {
      return;
    }

    openProfileDrawerAndToggleTheme();
  }).observe(document, OBSERVER_OPTIONS);

  darkQuery.addEventListener("change", () => {
    const elDarkToggle = getElDarkToggle();
    if (elDarkToggle) {
      const { activeElement } = document;
      elDarkToggle.click();
      activeElement.focus();
      return;
    }

    openProfileDrawerAndToggleTheme();
  });
})();