AI Studio Prompter

Maintains constant system instructions across new chats in Google's AI Studio.

当前为 2025-07-28 提交的版本,查看 最新版本

// ==UserScript==
// @name         AI Studio Prompter
// @version      0.2
// @description  Maintains constant system instructions across new chats in Google's AI Studio.
// @author       MJE43
// @license      MIT
// @match        https://aistudio.google.com/prompts/*
// @grant        none
// @namespace https://greasyfork.org/users/1499286
// ==/UserScript==

(function() {
    'use strict';

    // --- 1. EDIT YOUR SYSTEM INSTRUCTIONS HERE ---
    const systemInstructions = `
You are a helpful and friendly assistant.
Your primary goal is to provide accurate and concise information.
Format all responses using markdown for clarity.
    `.trim();
    // -------------------------------------------

    let mainInterval = null;

    function populateInstructions() {
        // Stop any previous timers to avoid multiple running at once
        if (mainInterval) {
            clearInterval(mainInterval);
        }

        // This function will run repeatedly until it finds the text area
        mainInterval = setInterval(() => {
            // Find the text area using the unique 'aria-label' you provided
            const instructionTextArea = document.querySelector('textarea[aria-label="System instructions"]');

            // If the text area is found AND it's currently empty...
            if (instructionTextArea && instructionTextArea.value === '') {
                console.log('Tampermonkey: Found empty System Instructions field. Populating now.');

                // 1. Set the text
                instructionTextArea.value = systemInstructions;

                // 2. "Dispatch an event" - This is a crucial step that programmatically tells
                // the website that the text has been changed, just as if a user typed it.
                const inputEvent = new Event('input', { bubbles: true });
                instructionTextArea.dispatchEvent(inputEvent);

                // 3. Stop checking now that the job is done for this page.
                clearInterval(mainInterval);
                mainInterval = null;
            }
            // If the text area is found but ALREADY has text, we do nothing and stop checking.
            else if (instructionTextArea && instructionTextArea.value !== '') {
                clearInterval(mainInterval);
                mainInterval = null;
            }
        }, 750); // Check for the element every 750 milliseconds
    }

    // --- SCRIPT EXECUTION ---

    // This part handles navigation within AI Studio (e.g., clicking "New chat").
    // It watches for a URL change and re-runs our function.
    let lastUrl = location.href;
    new MutationObserver(() => {
        const url = location.href;
        if (url !== lastUrl) {
            lastUrl = url;
            // Give the new page a moment to load its elements before we start checking
            setTimeout(populateInstructions, 500);
        }
    }).observe(document.body, { subtree: true, childList: true });

    // This runs the function once for the initial page load.
    // We wait a bit longer here to make sure the whole application is ready.
    setTimeout(populateInstructions, 1500);

})();