Gemini Auto Model Selector

Automatically select default model (thinking). You can choose default model in my userscript.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Gemini Auto Model Selector
// @namespace    http://greasyfork.org/
// @version      1.7
// @description  Automatically select default model (thinking). You can choose default model in my userscript.
// @author       Bui Quoc Dung
// @match        https://gemini.google.com/*
// @grant        none
// @changelog    v1.7: Pause auto-switching when user manually selects a model until URL changes
// ==/UserScript==
(function() {
    'use strict';
    if (window.self !== window.top) return;

    // ==================== MODEL MAPPING ====================
    const MODEL_MAP = {
        0: { keywords: ["Fast", "Nhanh"] },
        1: { keywords: ["Thinking", "Tư duy"] },
        2: { keywords: ["Pro"] }
    };

    // ==================== SITE CONFIG ====================
    const siteConfig = {
        selectedModelIndex: 1,
        selectors: {
            pickerButton: 'button[data-test-id="bard-mode-menu-button"]',
            menuButtons: 'button[role="menuitemradio"]'
        },
        timings: {
            menuOpenDelay: 500,
            cooldown: 2000
        }
    };

    let isSwitching = false;
    let userManualSelection = false;
    let currentURL = window.location.href;

    // ==================== DETECT USER MANUAL CLICK ====================
    function setupUserClickDetection() {
        document.addEventListener('click', (e) => {
            const pickerBtn = document.querySelector(siteConfig.selectors.pickerButton);
            const menuBtn = e.target.closest('button[role="menuitemradio"]');

            // If user clicks picker button or menu item
            if ((pickerBtn && pickerBtn.contains(e.target)) || menuBtn) {
                if (!isSwitching) { // Only mark if NOT script auto-switching
                    userManualSelection = true;
                }
            }
        }, true);
    }

    // ==================== DETECT URL CHANGE ====================
    function setupURLObserver() {
        const urlObserver = new MutationObserver(() => {
            const newURL = window.location.href;
            if (newURL !== currentURL) {
                currentURL = newURL;
                userManualSelection = false; // Reset when URL changes
                setTimeout(() => checkAndSwitch(), 300);
            }
        });

        urlObserver.observe(document.querySelector('title'), {
            childList: true,
            subtree: true
        });

        // Fallback: use setInterval to check URL
        setInterval(() => {
            const newURL = window.location.href;
            if (newURL !== currentURL) {
                currentURL = newURL;
                userManualSelection = false;
                setTimeout(() => checkAndSwitch(), 300);
            }
        }, 1000);
    }

    // ==================== AUTO SWITCH LOGIC ====================
    function checkAndSwitch() {
        if (isSwitching || userManualSelection) return;

        const pickerBtn = document.querySelector(siteConfig.selectors.pickerButton);
        if (!pickerBtn) return;

        const targetModel = MODEL_MAP[siteConfig.selectedModelIndex];
        const currentLabel = pickerBtn.innerText.trim();
        const isCorrect = targetModel.keywords.some(k => currentLabel.includes(k));

        if (isCorrect) return;

        isSwitching = true;
        pickerBtn.click();

        setTimeout(() => {
            const buttons = document.querySelectorAll(siteConfig.selectors.menuButtons);
            const targetBtn = buttons[siteConfig.selectedModelIndex];

            if (targetBtn) {
                targetBtn.click();
            } else {
                document.body.click();
            }

            setTimeout(() => { isSwitching = false; }, siteConfig.timings.cooldown);
        }, siteConfig.timings.menuOpenDelay);
    }

    // ==================== INIT ====================
    setupUserClickDetection();
    setupURLObserver();

    const observer = new MutationObserver(() => {
        if (!userManualSelection) {
            checkAndSwitch();
        }
    });
    observer.observe(document.body, { childList: true, subtree: true });

    checkAndSwitch();
})();