Skip to content

Instantly share code, notes, and snippets.

@eai04191
Last active March 24, 2026 09:22
Show Gist options
  • Select an option

  • Save eai04191/12e0975933e6d7edced3dbd4bd220717 to your computer and use it in GitHub Desktop.

Select an option

Save eai04191/12e0975933e6d7edced3dbd4bd220717 to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name Gemini Auto Switch to Pro (Optimized)
// @namespace http://tampermonkey.net/
// @version 2.1
// @description Geminiの新規チャット時にdata-test-idを使用してProモードへ自動切り替えし、入力欄にフォーカスする
// @author You
// @match https://gemini.google.com/*
// @grant none
// @run-at document-idle
// ==/UserScript==
(function() {
'use strict';
// 切り替えたいモデル名のテキスト(画面表示に合わせる)
const TARGET_MODEL_TEXT = "Pro";
// セレクタ定義
const TRIGGER_SELECTOR = '[data-test-id="bard-mode-menu-button"]';
const TEXTBOX_SELECTOR = '[role="textbox"]';
let isProcessing = false;
// テキストボックスにフォーカスする関数
function focusTextbox() {
const textbox = document.querySelector(TEXTBOX_SELECTOR);
if (textbox) {
textbox.focus();
}
}
function attemptSwitch() {
if (isProcessing) return;
// 1. トリガーとなるボタンコンテナを探す
const triggerContainer = document.querySelector(TRIGGER_SELECTOR);
if (!triggerContainer) return;
// 2. 現在の表示テキストを確認
// 既に "Pro" が含まれていれば、フォーカスだけ当てて終了
if (triggerContainer.textContent.includes(TARGET_MODEL_TEXT)) {
focusTextbox();
return;
}
// 3. 切り替え処理開始
isProcessing = true;
console.log(`[Gemini Auto Switch] Switching to ${TARGET_MODEL_TEXT}...`);
// コンテナ内のbutton要素をクリック(またはコンテナ自体をクリック)
const buttonToClick = triggerContainer.querySelector('button') || triggerContainer;
buttonToClick.click();
// 4. メニューが開くのを待って選択
setTimeout(() => {
// メニュー項目を探す (role="menuitem" または role="option")
const menuItems = document.querySelectorAll('[role="menuitem"], [role="option"], .mat-mdc-menu-item');
let targetFound = false;
for (const item of menuItems) {
// 余計な空白を除去して比較
if (item.textContent.trim().includes(TARGET_MODEL_TEXT)) {
item.click();
targetFound = true;
console.log(`[Gemini Auto Switch] Switched to ${TARGET_MODEL_TEXT}`);
// モデル切り替え後、メニューが閉じるのを少し待ってからフォーカス
setTimeout(focusTextbox, 100);
break;
}
}
if (!targetFound) {
// 見つからなかった場合はメニューを閉じるためにもう一度ボタンを押す
buttonToClick.click();
}
// 処理完了後のクールダウン(連続実行防止)
setTimeout(() => { isProcessing = false; }, 1000);
}, 300); // メニューのアニメーション待ち時間
}
// SPA遷移の監視 (MutationObserver)
const observer = new MutationObserver((mutations) => {
// 新規チャット画面 (/app または /) の場合のみ実行
const isNewChat = window.location.pathname === "/app" || window.location.pathname === "/";
if (isNewChat) {
attemptSwitch();
}
});
observer.observe(document.body, { childList: true, subtree: true });
// 初回実行
setTimeout(attemptSwitch, 1000);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment