Skip to content

armgit / Pake

Public Imported from GitHub
0

Turn any website into a lightweight desktop app with one command, on macOS, Windows and Linux.

2.9 KB
Raw Blame
(function () {
  if (window.top && window.top !== window) return;
  if (window.pakeConfig?.dark_mode) return;

  document.addEventListener("DOMContentLoaded", () => {
    if (window.pakeConfig?.dark_mode) return;

    const debounce = (func, wait) => {
      let timeout;
      return (...args) => {
        clearTimeout(timeout);
        timeout = setTimeout(() => func(...args), wait);
      };
    };

    const mediaQuery =
      typeof window.matchMedia === "function"
        ? window.matchMedia("(prefers-color-scheme: dark)")
        : null;

    const systemMode = () => (mediaQuery?.matches ? "dark" : "light");

    const detectPageMode = () => {
      const doc = document.documentElement;
      if (!doc) return null;
      const body = document.body;

      // Check for explicit theme classes or attributes
      const isDark =
        doc.classList.contains("dark") ||
        body?.classList?.contains("dark") ||
        doc.getAttribute("data-theme") === "dark" ||
        body?.getAttribute?.("data-theme") === "dark" ||
        doc.style.colorScheme === "dark";

      const isLight =
        doc.classList.contains("light") ||
        body?.classList?.contains("light") ||
        doc.getAttribute("data-theme") === "light" ||
        body?.getAttribute?.("data-theme") === "light" ||
        doc.style.colorScheme === "light";

      if (isDark) return "dark";
      if (isLight) return "light";
      return null;
    };

    let appliedMode = null;

    const sendThemeMode = (mode) => {
      const invoke = window.__PAKE_NATIVE__?.core?.invoke;
      if (!invoke) return;
      Promise.resolve(invoke("update_theme_mode", { mode })).catch(() => {});
    };

    const updateTheme = () => {
      if (window.pakeConfig?.dark_mode) return;
      const mode = detectPageMode();

      if (mode === null) {
        if (appliedMode !== null) {
          appliedMode = null;
          sendThemeMode("system");
        }
        return;
      }

      // While following the OS appearance, skip pinning a mode that already
      // matches the OS so native appearance switches keep working.
      if (appliedMode === null && mode === systemMode()) {
        return;
      }

      if (mode === appliedMode) {
        return;
      }

      appliedMode = mode;
      sendThemeMode(mode);
    };

    const debouncedUpdateTheme = debounce(updateTheme, 200);

    // Initial check with delay to allow site to render
    setTimeout(updateTheme, 500);

    // Watch for DOM changes
    const observer = new MutationObserver(debouncedUpdateTheme);
    const config = {
      attributes: true,
      attributeFilter: ["class", "data-theme", "style"],
      subtree: false,
    };

    if (document.documentElement) {
      observer.observe(document.documentElement, config);
    }
    if (document.body) {
      observer.observe(document.body, config);
    }

    mediaQuery?.addEventListener?.("change", updateTheme);
  });
})();