Advertisement
AlanDias17

Userscript: AI Studio Auto-Fill & Collapse System Instructions

Jun 3rd, 2025 (edited)
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 5.74 KB | Source Code | 0 0
  1. // ==UserScript==
  2. // @name         AI Studio: Auto-Fill & Collapse on New Chat
  3. // @author       Alan Dias (c)2025
  4. // @version      1.4
  5. // @description  Whenever you open or switch to a new Chat in Google AI Studio, this script auto-clicks “System instructions,”
  6.  //              injects your saved prompt text, then collapses the panel again.
  7. // @match        https://aistudio.google.com/*
  8. // @grant        none
  9. // ==/UserScript==
  10.  
  11. (function () {
  12.     'use strict';
  13.  
  14.     // ────────────────────────────────────────────────────────────────────────────
  15.     // 👇 Edit this constant to whatever “System Instructions” text you want:
  16.     const SYSTEM_INSTRUCTIONS = `
  17. You are a top‐tier, elite programmer.Provide a precise short answer only.
  18. `.trim();
  19.     // ────────────────────────────────────────────────────────────────────────────
  20.  
  21.     // How often (ms) to retry internally when waiting for textarea to appear
  22.     const CHECK_INTERVAL_MS = 200;
  23.     // Maximum number of retries when injecting per attempt
  24.     const MAX_INNER_ATTEMPTS = 20;
  25.  
  26.     // Internal flag to prevent overlapping jobs
  27.     let injectionInProgress = false;
  28.  
  29.     /**
  30.      * Attempts to open the System Instructions panel, inject text, then collapse it.
  31.      * Returns immediately if another injection is in progress, or if the textarea already
  32.      * contains the exact SYSTEM_INSTRUCTIONS.
  33.      */
  34.     function openInjectCollapse() {
  35.         if (injectionInProgress) {
  36.             return;
  37.         }
  38.         injectionInProgress = true;
  39.  
  40.         // 1) Find the “System instructions” button by its aria-label
  41.         const toggleButton = document.querySelector('button[aria-label="System instructions"]');
  42.         if (!toggleButton) {
  43.             // If button is not found, bail out and allow MutationObserver to try again later.
  44.             injectionInProgress = false;
  45.             return;
  46.         }
  47.  
  48.         // 2) Click to open the panel
  49.         toggleButton.click();
  50.         console.log('[Userscript] ▶ Opened System Instructions panel.');
  51.  
  52.         // 3) Now wait until the textarea appears, up to MAX_INNER_ATTEMPTS times
  53.         let innerAttempts = 0;
  54.         const innerInterval = setInterval(() => {
  55.             innerAttempts++;
  56.             // a) Look for the <textarea aria-label="System instructions">
  57.             const textarea = document.querySelector('textarea[aria-label="System instructions"]');
  58.             if (textarea) {
  59.                 // b) If its value is already exactly our instructions, skip reinjection
  60.                 if (textarea.value.trim() !== SYSTEM_INSTRUCTIONS) {
  61.                     textarea.value = SYSTEM_INSTRUCTIONS;
  62.                     // Fire an input event so AI Studio notices the change
  63.                     textarea.dispatchEvent(new Event('input', { bubbles: true }));
  64.                     console.log('[Userscript] ✅ Injected new System Instructions.');
  65.                 } else {
  66.                     console.log('[Userscript] ☑ Text already present; skipping injection.');
  67.                 }
  68.  
  69.                 // c) Wait a tiny bit so Angular can register the change, then collapse
  70.                 setTimeout(() => {
  71.                     toggleButton.click();
  72.                     console.log('[Userscript] ⏬ Collapsed System Instructions panel.');
  73.                     clearInterval(innerInterval);
  74.                     injectionInProgress = false;
  75.                 }, 150);
  76.                 return;
  77.             }
  78.  
  79.             // d) If we've retried too many times, give up and reset
  80.             if (innerAttempts >= MAX_INNER_ATTEMPTS) {
  81.                 console.warn('[Userscript] ⚠️ Gave up waiting for textarea.');
  82.                 clearInterval(innerInterval);
  83.                 injectionInProgress = false;
  84.             }
  85.         }, CHECK_INTERVAL_MS);
  86.     }
  87.  
  88.     // ────────────────────────────────────────────────────────────────────────────
  89.     // 1) Run once on script load (in case you landed directly on a Chat page)
  90.     openInjectCollapse();
  91.  
  92.     // 2) Observe DOM mutations so that, when AI Studio does a client-side “New Chat”
  93.     //    (or any re-render) we automatically run `openInjectCollapse()` again.
  94.     const observer = new MutationObserver((mutations) => {
  95.         for (const m of mutations) {
  96.             // If a new “System instructions” button node appears, trigger injection.
  97.             // We check addedNodes for any element matching our button selector.
  98.             for (const node of m.addedNodes) {
  99.                 if (!(node instanceof HTMLElement)) continue;
  100.                 // If this exact node or any child of it matches the button, invoke
  101.                 if (node.matches?.('button[aria-label="System instructions"]') ||
  102.                     node.querySelector?.('button[aria-label="System instructions"]')) {
  103.                     openInjectCollapse();
  104.                     return;
  105.                 }
  106.             }
  107.         }
  108.     });
  109.  
  110.     // Start observing the entire <body> for subtree modifications:
  111.     observer.observe(document.body, {
  112.         childList: true,
  113.         subtree: true
  114.     });
  115.     // ────────────────────────────────────────────────────────────────────────────
  116.  
  117. })();
  118.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement