Gemini force 2.0 Flash

Auto change 1.5 to 2.0 (Google may change it later)

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name        Gemini force 2.0 Flash
// @namespace   Violentmonkey Scripts
// @description Auto change 1.5 to 2.0 (Google may change it later)
// @match       *://gemini.google.com/*
// @grant       none
// @version     1.2
// @license MIT
// @author      Artin
// @description 12/15/2024, 9:35:58 PM
// ==/UserScript==

(function() {
    'use strict';

    function checkAndClick() {
        const spanElement = document.querySelector("#app-root > main > div > bard-mode-switcher > button > span.mdc-button__label > div.current-mode-title.ng-star-inserted > span");
        if (spanElement && spanElement.textContent.trim() === '1.5 Flash') {
            const firstButton = document.querySelector('#app-root > main > div > bard-mode-switcher > button');
            if (firstButton) {
                firstButton.click();
                setTimeout(() => {
                    const thirdButton = document.querySelector('#mat-menu-panel-0 > div > button:nth-child(3)');
                    if (thirdButton) {
                        thirdButton.click();
                        setTimeout(() => {
                            const temp = document.createElement('div');
                            temp.tabIndex = -1;
                            document.body.appendChild(temp);
                            temp.focus();
                            document.body.removeChild(temp);
                        }, 200);
                    }
                }, 200);
            }
        }
    }

    // 檢查 URL 是否符合條件
    function shouldExecute() {
        return window.location.pathname === '/app';
    }

    // 設置觀察者和 URL 變化監聽
    function setupObserver() {
        let hasExecuted = false;
        const targetNode = document.querySelector('#app-root');

        if (targetNode) {
            const observer = new MutationObserver(() => {
                if (!hasExecuted && shouldExecute()) {
                    hasExecuted = true;
                    checkAndClick();
                    observer.disconnect();
                }
            });

            observer.observe(targetNode, {
                childList: true,
                subtree: true,
                characterData: true
            });
        }
    }

    // 監聽 URL 變化
    function initUrlChangeListener() {
        let lastPath = window.location.pathname;

        // 監聽 pushState 和 replaceState
        const originalPushState = history.pushState;
        const originalReplaceState = history.replaceState;

        history.pushState = function() {
            originalPushState.apply(this, arguments);
            if (lastPath.startsWith('/app/') && window.location.pathname === '/app') {
                setupObserver();
            }
            lastPath = window.location.pathname;
        };

        history.replaceState = function() {
            originalReplaceState.apply(this, arguments);
            if (lastPath.startsWith('/app/') && window.location.pathname === '/app') {
                setupObserver();
            }
            lastPath = window.location.pathname;
        };

        // 監聽瀏覽器的前進/後退
        window.addEventListener('popstate', () => {
            if (lastPath.startsWith('/app/') && window.location.pathname === '/app') {
                setupObserver();
            }
            lastPath = window.location.pathname;
        });
    }

    // 當頁面載入完成時開始執行
    window.addEventListener('load', () => {
        if (shouldExecute()) {
            setupObserver();
        }
        initUrlChangeListener();
    });
})();