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 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 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();

})();