Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ==UserScript==
- // @name AI Studio: Auto-Fill & Collapse on New Chat
- // @author Alan Dias (c)2025
- // @version 1.4
- // @description Whenever you open or switch to a new Chat in Google AI Studio, this script auto-clicks “System instructions,”
- // injects your saved prompt text, then collapses the panel again.
- // @match https://aistudio.google.com/*
- // @grant none
- // ==/UserScript==
- (function () {
- 'use strict';
- // ────────────────────────────────────────────────────────────────────────────
- // 👇 Edit this constant to whatever “System Instructions” text you want:
- const SYSTEM_INSTRUCTIONS = `
- You are a top‐tier, elite programmer.Provide a precise short answer only.
- `.trim();
- // ────────────────────────────────────────────────────────────────────────────
- // How often (ms) to retry internally when waiting for textarea to appear
- const CHECK_INTERVAL_MS = 200;
- // Maximum number of retries when injecting per attempt
- const MAX_INNER_ATTEMPTS = 20;
- // Internal flag to prevent overlapping jobs
- let injectionInProgress = false;
- /**
- * Attempts to open the System Instructions panel, inject text, then collapse it.
- * Returns immediately if another injection is in progress, or if the textarea already
- * contains the exact SYSTEM_INSTRUCTIONS.
- */
- function openInjectCollapse() {
- if (injectionInProgress) {
- return;
- }
- injectionInProgress = true;
- // 1) Find the “System instructions” button by its aria-label
- const toggleButton = document.querySelector('button[aria-label="System instructions"]');
- if (!toggleButton) {
- // If button is not found, bail out and allow MutationObserver to try again later.
- injectionInProgress = false;
- return;
- }
- // 2) Click to open the panel
- toggleButton.click();
- console.log('[Userscript] ▶ Opened System Instructions panel.');
- // 3) Now wait until the textarea appears, up to MAX_INNER_ATTEMPTS times
- let innerAttempts = 0;
- const innerInterval = setInterval(() => {
- innerAttempts++;
- // a) Look for the <textarea aria-label="System instructions">
- const textarea = document.querySelector('textarea[aria-label="System instructions"]');
- if (textarea) {
- // b) If its value is already exactly our instructions, skip reinjection
- if (textarea.value.trim() !== SYSTEM_INSTRUCTIONS) {
- textarea.value = SYSTEM_INSTRUCTIONS;
- // Fire an input event so AI Studio notices the change
- textarea.dispatchEvent(new Event('input', { bubbles: true }));
- console.log('[Userscript] ✅ Injected new System Instructions.');
- } else {
- console.log('[Userscript] ☑ Text already present; skipping injection.');
- }
- // c) Wait a tiny bit so Angular can register the change, then collapse
- setTimeout(() => {
- toggleButton.click();
- console.log('[Userscript] ⏬ Collapsed System Instructions panel.');
- clearInterval(innerInterval);
- injectionInProgress = false;
- }, 150);
- return;
- }
- // d) If we've retried too many times, give up and reset
- if (innerAttempts >= MAX_INNER_ATTEMPTS) {
- console.warn('[Userscript] ⚠️ Gave up waiting for textarea.');
- clearInterval(innerInterval);
- injectionInProgress = false;
- }
- }, CHECK_INTERVAL_MS);
- }
- // ────────────────────────────────────────────────────────────────────────────
- // 1) Run once on script load (in case you landed directly on a Chat page)
- openInjectCollapse();
- // 2) Observe DOM mutations so that, when AI Studio does a client-side “New Chat”
- // (or any re-render) we automatically run `openInjectCollapse()` again.
- const observer = new MutationObserver((mutations) => {
- for (const m of mutations) {
- // If a new “System instructions” button node appears, trigger injection.
- // We check addedNodes for any element matching our button selector.
- for (const node of m.addedNodes) {
- if (!(node instanceof HTMLElement)) continue;
- // If this exact node or any child of it matches the button, invoke
- if (node.matches?.('button[aria-label="System instructions"]') ||
- node.querySelector?.('button[aria-label="System instructions"]')) {
- openInjectCollapse();
- return;
- }
- }
- }
- });
- // Start observing the entire <body> for subtree modifications:
- observer.observe(document.body, {
- childList: true,
- subtree: true
- });
- // ────────────────────────────────────────────────────────────────────────────
- })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement