Skip to content

Instantly share code, notes, and snippets.

@hushin
Last active December 18, 2025 04:53
Show Gist options
  • Select an option

  • Save hushin/43888eed98d8c45d0ba400e8718fd147 to your computer and use it in GitHub Desktop.

Select an option

Save hushin/43888eed98d8c45d0ba400e8718fd147 to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name Gemini Auto Submit from URL Query
// @namespace http://tampermonkey.net/
// @version 1.0
// @description Automatically submit prompt from URL query parameter 'q' to Gemini
// @author hushin
// @match https://gemini.google.com/app*
// @grant none
// ==/UserScript==
(function () {
'use strict';
function injectPromptAndSubmit(promptText) {
try {
const editor = document.querySelector('.ql-editor.textarea');
if (!editor) {
console.error('テキストエリアが見つかりません。');
return;
}
// プロンプトを入力(TrustedHTML対応)
editor.focus();
editor.textContent = '';
const lines = promptText.split('\n');
lines.forEach((line, index) => {
const p = document.createElement('p');
p.textContent = line;
editor.appendChild(p);
});
// 送信ボタンをクリック
const sendButton = document.querySelector(
'button[aria-label="プロンプトを送信"], button[aria-label="Send"], button[aria-label="送信"]'
);
if (!sendButton) {
console.error('送信ボタンが見つかりません。');
return;
}
setTimeout(() => {
sendButton.click();
// ページ遷移を監視してクエリを削除
waitForPageTransition();
}, 100);
} catch (error) {
console.error('Geminiへの入力中にエラーが発生しました:', error);
}
}
function waitForPageTransition() {
const checkTransition = setInterval(() => {
const currentUrl = window.location.href;
// /app/(hash値)?q=... のパターンにマッチするか確認
if (/\/app\/[a-f0-9]+\?.*q=/.test(currentUrl)) {
clearInterval(checkTransition);
// 少し待ってからクエリを削除
setTimeout(() => {
removeQueryFromURL();
}, 10_000);
}
}, 100);
// 1分後にタイムアウト
setTimeout(() => {
clearInterval(checkTransition);
}, 60000);
}
function removeQueryFromURL() {
const url = new URL(window.location.href);
url.searchParams.delete('q');
window.history.replaceState({}, '', url);
}
function init() {
const urlParams = new URLSearchParams(window.location.search);
const query = urlParams.get('q');
if (query) {
const decodedQuery = decodeURIComponent(query);
// Gemini のエディタが読み込まれるまで待機
const checkEditor = setInterval(() => {
const editor = document.querySelector('.ql-editor.textarea');
if (editor) {
clearInterval(checkEditor);
injectPromptAndSubmit(decodedQuery);
}
}, 500);
// 10秒後にタイムアウト
setTimeout(() => {
clearInterval(checkEditor);
}, 10000);
}
}
// ページ読み込み後に実行
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', init);
} else {
init();
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment