Automatically select default model (thinking). You can choose default model in my userscript.
// ==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();
})();