Gemini Model Memory

Automatically select default model. Remembers user's choice.

K instalaci tototo skriptu si budete muset nainstalovat rozšíření jako Tampermonkey, Greasemonkey nebo Violentmonkey.

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

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Userscripts.

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

K instalaci tohoto skriptu si budete muset nainstalovat manažer uživatelských skriptů.

(Už mám manažer uživatelských skriptů, nechte mě ho nainstalovat!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Už mám manažer uživatelských stylů, nechte mě ho nainstalovat!)

// ==UserScript==
// @name         Gemini Model Memory
// @namespace    http://greasyfork.org/
// @version      1.9
// @description  Automatically select default model. Remembers user's choice.
// @author       Bui Quoc Dung
// @match        https://gemini.google.com/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';
    if (window.self !== window.top) return;

    const siteConfig = {
        selectors: {
            pickerBtn: 'button[data-test-id="bard-mode-menu-button"]',
            menuItems: '[role="menuitemradio"], [role="menuitem"]'
        }
    };

    const timings = {
        interval: 1000,
        renderDelay: 600,
        cooldown: 1500
    };

    const MODELS = {
        0: { keywords: ["Flash", "Nhanh", "Fast"] },
        1: { keywords: ["Thinking", "Tư duy"] },
        2: { keywords: ["Pro", "Advanced"] }
    };

    let savedIndex = localStorage.getItem('gemini_saved_model_index');
    let targetModelId = (savedIndex !== null && MODELS[savedIndex]) ? parseInt(savedIndex) : 1;

    let isSwitching = false;
    let userManualSelection = false;

    function isAppHomePage() {
        const regex = /^https:\/\/gemini\.google\.com\/(u\/\d+\/)?app\/?([?#].*)?$/;
        return regex.test(window.location.href);
    }

    document.addEventListener('click', (e) => {
        if (isSwitching) return;

        const pickerBtn = document.querySelector(siteConfig.selectors.pickerBtn);
        const item = e.target.closest(siteConfig.selectors.menuItems);

        if (item) {
            const itemText = item.innerText || item.textContent || "";
            for (let key in MODELS) {
                if (MODELS[key].keywords.some(k => itemText.includes(k))) {
                    targetModelId = parseInt(key);
                    localStorage.setItem('gemini_saved_model_index', key);
                    break;
                }
            }
            userManualSelection = false;
        } else if (pickerBtn && pickerBtn.contains(e.target)) {
            userManualSelection = true;
        } else {
            userManualSelection = false;
        }
    }, true);

    setInterval(() => {
        if (!isAppHomePage() || isSwitching || userManualSelection) return;

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

        const currentText = pickerBtn.innerText || pickerBtn.textContent || "";
        const targetModel = MODELS[targetModelId];

        if (!targetModel) return;
        if (targetModel.keywords.some(k => currentText.includes(k))) return;

        isSwitching = true;
        pickerBtn.click();

        setTimeout(() => {
            const menuItems = document.querySelectorAll(siteConfig.selectors.menuItems);
            let clicked = false;

            for (let item of menuItems) {
                if (item.getBoundingClientRect().width > 0) {
                    const itemText = item.innerText || item.textContent || "";
                    if (targetModel.keywords.some(k => itemText.includes(k))) {
                        item.click();
                        clicked = true;
                        break;
                    }
                }
            }

            if (!clicked) document.body.click();

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

        }, timings.renderDelay);
    }, timings.interval);

})();