Force Bing Dark Mode

Automatically selects dark mode in Bing settings

目前為 2025-04-07 提交的版本,檢視 最新版本

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

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

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

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

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

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

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Force Bing Dark Mode
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  Automatically selects dark mode in Bing settings
// @author       Henry Suen
// @match        *://*.bing.com/*
// @grant        none
// @license MIT
// @run-at       document-idle
// ==/UserScript==

(function() {
    'use strict';

    // Save the dark mode preference in cookies and localStorage
    function saveDarkModePreference() {
        // Set cookie with dark mode preference (expires in 365 days)
        document.cookie = "_UR=QS=0&TQS=0&ThemeID=dark; path=/; domain=.bing.com; max-age=" + 60*60*24*365;

        // Also save to localStorage
        if (localStorage) {
            localStorage.setItem('_UR', '{"TQS":"0","QS":"0","ThemeID":"dark"}');
        }
    }

    // Click the dark mode radio button if it exists and isn't already selected
    function selectDarkMode() {
        // Find the dark mode radio button by its specific ID
        const darkRadio = document.querySelector('#rdiodark');

        if (darkRadio && !darkRadio.checked) {
            console.log('Bing Force Dark Mode: Found dark mode option, clicking it');
            darkRadio.click();
            return true;
        }

        // Alternative selector using the value and name attributes
        const darkRadioAlt = document.querySelector('input[type="radio"][name="da_rdio"][value="1"]');

        if (darkRadioAlt && !darkRadioAlt.checked) {
            console.log('Bing Force Dark Mode: Found dark mode option (alt method), clicking it');
            darkRadioAlt.click();
            return true;
        }

        return false;
    }

    // Attempt to find the appearance section and expand it if needed
    function findAndExpandAppearanceSection() {
        // Look for the appearance section by its class and content
        const appearanceSection = document.querySelector('div[class*="hb_section hb_top_sec hb_expanded"][aria-controls="hbradiobtn"]');

        if (!appearanceSection) {
            // If not expanded, find the appearance dropdown
            const appearanceDropdown = document.querySelector('div.hb_title_col:contains("Appearance")') ||
                                      document.querySelector('div:contains("Appearance")').closest('.hb_titlerow');

            if (appearanceDropdown) {
                const expandButton = appearanceDropdown.closest('[aria-expanded="false"]');
                if (expandButton) {
                    console.log('Bing Force Dark Mode: Expanding appearance section');
                    expandButton.click();
                    return true;
                }
            }
        } else {
            // Appearance section is already expanded
            return true;
        }

        return false;
    }

    // Main function to force dark mode
    function forceDarkMode() {
        // First save the preference
        saveDarkModePreference();

        // Try direct selection
        if (selectDarkMode()) {
            console.log('Bing Force Dark Mode: Successfully applied dark mode');
            return;
        }

        // If we couldn't find the dark mode option directly, try to open the appearance section
        if (findAndExpandAppearanceSection()) {
            // Wait for the section to expand
            setTimeout(() => {
                if (selectDarkMode()) {
                    console.log('Bing Force Dark Mode: Successfully applied dark mode after expanding section');
                }
            }, 300);
        }
    }

    // Settings menu may load dynamically, so we'll set up a MutationObserver
    const observer = new MutationObserver((mutations) => {
        for (const mutation of mutations) {
            if (mutation.addedNodes && mutation.addedNodes.length > 0) {
                // Look for settings dropdown being added to the DOM
                const settingsElements = document.querySelectorAll('div[class*="hb_section hb_top_sec"]');
                if (settingsElements.length > 0) {
                    forceDarkMode();
                    // Once we've found and processed the settings, we can disconnect the observer
                    observer.disconnect();
                    break;
                }
            }
        }
    });

    // Start observing for the settings panel
    observer.observe(document.body, { childList: true, subtree: true });

    // Also run after the page has loaded
    window.addEventListener('load', () => {
        setTimeout(forceDarkMode, 1000);
    });

    // Run immediately too in case the page is already loaded
    saveDarkModePreference();
    setTimeout(forceDarkMode, 500);
})();