Gemini Dark Mode Sync (LocalStorage Method)

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

当前为 2025-04-20 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==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();

})();