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 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name Gemini Dark Mode Sync (LocalStorage Method)
  3. // @namespace http://www.jeffbyers.com
  4. // @namespace https://github.com/nullstreak
  5. // @match https://gemini.google.com/*
  6. // @grant none
  7. // @version 6.0
  8. // @author Jeff Byers <jeff@jeffbyers.com>, nullstreak
  9. // @license GPLv3 - http://www.gnu.org/licenses/gpl-3.0.txt
  10. // @copyright Copyright (C) 2024, by Jeff Byers <jeff@jeffbyers.com>
  11. // @icon https://www.gstatic.com/lamda/images/gemini_favicon_f069958c85030456e93de685481c559f160ea06b.png
  12. // @description Synchronizes Google Gemini's dark mode with your system's preference by directly modifying Local Storage.
  13. // ==/UserScript==
  14.  
  15. (function () {
  16. 'use strict';
  17.  
  18. const THEME_KEY = 'Bard-Color-Theme';
  19. const DARK_VALUE = 'Bard-Dark-Theme';
  20. // --- IMPORTANT: Adjust this if Light mode uses a different value ---
  21. // Common alternatives might be '' (empty string) or removing the key entirely.
  22. // If removing the key is needed, modify the toggle function accordingly.
  23. const LIGHT_VALUE = 'Bard-Light-Theme';
  24. // ---
  25.  
  26. // Function to get the system's preferred color scheme
  27. const getPreferredScheme = () =>
  28. window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
  29.  
  30. // Function to set the theme in Local Storage and reload if changed
  31. const setGeminiTheme = (enableDarkMode) => {
  32. const targetValue = enableDarkMode ? DARK_VALUE : LIGHT_VALUE;
  33. const currentValue = localStorage.getItem(THEME_KEY);
  34.  
  35. if (currentValue !== targetValue) {
  36. console.log(`[Gemini Dark Mode Sync] System requires ${enableDarkMode ? 'dark' : 'light'} mode. Current is '${currentValue}', setting to '${targetValue}'. Reloading.`);
  37. localStorage.setItem(THEME_KEY, targetValue);
  38. // Reload the page to apply the theme change, as Gemini might only read this on load.
  39. location.reload();
  40. } else {
  41. // console.log(`[Gemini Dark Mode Sync] Theme already matches system preference (${enableDarkMode ? 'dark' : 'light'}). No change needed.`);
  42. }
  43. };
  44.  
  45. // Initial setup on page load
  46. const init = () => {
  47. const preferredScheme = getPreferredScheme();
  48. console.log(`[Gemini Dark Mode Sync] Initial check: System preference is ${preferredScheme}.`);
  49. setGeminiTheme(preferredScheme === 'dark');
  50. };
  51.  
  52. // Observe for changes in system preference
  53. const watchSystemPreference = () => {
  54. const mediaQuery = window.matchMedia('(prefers-color-scheme: dark)');
  55. mediaQuery.addEventListener('change', (e) => {
  56. console.log(`[Gemini Dark Mode Sync] System preference changed. Dark mode enabled: ${e.matches}`);
  57. setGeminiTheme(e.matches);
  58. });
  59. console.log('[Gemini Dark Mode Sync] Watching for system theme changes.');
  60. };
  61.  
  62. // Run initialization and start watching for changes
  63. init();
  64. watchSystemPreference();
  65.  
  66. })();