Gemini Dark Mode Sync (LocalStorage Method)

Synchronizes Google Gemini's dark mode with your system's preference by directly modifying Local Storage.

// ==UserScript==
// @name        Gemini Dark Mode Sync (LocalStorage Method)
// @namespace   http://www.jeffbyers.com
// @namespace   https://github.com/nullstreak
// @match       https://gemini.google.com/*
// @grant       none
// @version     6.0
// @author      Jeff Byers <[email protected]>, nullstreak
// @license     GPLv3 - http://www.gnu.org/licenses/gpl-3.0.txt
// @copyright   Copyright (C) 2024, by Jeff Byers <[email protected]>
// @icon        https://www.gstatic.com/lamda/images/gemini_favicon_f069958c85030456e93de685481c559f160ea06b.png
// @description Synchronizes Google Gemini's dark mode with your system's preference by directly modifying Local Storage.
// ==/UserScript==

(function () {
    'use strict';

    const THEME_KEY = 'Bard-Color-Theme';
    const DARK_VALUE = 'Bard-Dark-Theme';
    // --- IMPORTANT: Adjust this if Light mode uses a different value ---
    // Common alternatives might be '' (empty string) or removing the key entirely.
    // If removing the key is needed, modify the toggle function accordingly.
    const LIGHT_VALUE = 'Bard-Light-Theme';
    // ---

    // Function to get the system's preferred color scheme
    const getPreferredScheme = () =>
        window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';

    // Function to set the theme in Local Storage and reload if changed
    const setGeminiTheme = (enableDarkMode) => {
        const targetValue = enableDarkMode ? DARK_VALUE : LIGHT_VALUE;
        const currentValue = localStorage.getItem(THEME_KEY);

        if (currentValue !== targetValue) {
            console.log(`[Gemini Dark Mode Sync] System requires ${enableDarkMode ? 'dark' : 'light'} mode. Current is '${currentValue}', setting to '${targetValue}'. Reloading.`);
            localStorage.setItem(THEME_KEY, targetValue);
            // Reload the page to apply the theme change, as Gemini might only read this on load.
            location.reload();
        } else {
            // console.log(`[Gemini Dark Mode Sync] Theme already matches system preference (${enableDarkMode ? 'dark' : 'light'}). No change needed.`);
        }
    };

    // Initial setup on page load
    const init = () => {
        const preferredScheme = getPreferredScheme();
        console.log(`[Gemini Dark Mode Sync] Initial check: System preference is ${preferredScheme}.`);
        setGeminiTheme(preferredScheme === 'dark');
    };

    // Observe for changes in system preference
    const watchSystemPreference = () => {
        const mediaQuery = window.matchMedia('(prefers-color-scheme: dark)');
        mediaQuery.addEventListener('change', (e) => {
            console.log(`[Gemini Dark Mode Sync] System preference changed. Dark mode enabled: ${e.matches}`);
            setGeminiTheme(e.matches);
        });
         console.log('[Gemini Dark Mode Sync] Watching for system theme changes.');
    };

    // Run initialization and start watching for changes
    init();
    watchSystemPreference();

})();