Greasy Fork 支持简体中文。

Google AI Studio Sidebar Controller

Auto collapse sidebars in Google AI Studio. 原网页总是莫名弹出左右栏,非常反人类。The script will automatically activate the grounding(google search) feature for new chats.

// ==UserScript==
// @name         Google AI Studio Sidebar Controller
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  Auto collapse sidebars in Google AI Studio. 原网页总是莫名弹出左右栏,非常反人类。The script will automatically activate the grounding(google search) feature for new chats.
// @icon         https://www.gstatic.com/aistudio/ai_studio_favicon_256x256.png
// @author       qianjunlang
// @match        https://aistudio.google.com/*
// @grant        none
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    const DEBOUNCE_WAIT = 150;
    const SELECTORS = {
        SIDEBAR: '.layout-navbar.ng-trigger-parent.expanded',
        TOGGLE_BUTTON1: 'button[aria-label="Expand or collapse navigation menu"]',
        TOGGLE_BUTTON2: 'button[aria-label="Expand or collapse run settings panel"]'
    };

    const collapseSidebar = () => {
        const expandedSidebar = document.querySelector(SELECTORS.SIDEBAR);
        if ( !expandedSidebar || !window.location.href.includes('/prompts/') ) return;

        expandedSidebar.querySelector(SELECTORS.TOGGLE_BUTTON1).click();

        if (window.location.href.endsWith('/new_chat')){
            const searchToggle = document.querySelector('.search-as-a-tool-toggle button');
            if (searchToggle && !document.querySelector('.search-source')) searchToggle.click();

        } else if ( document.querySelector('.footer.run-settings-expanded') )
            document.querySelector(SELECTORS.TOGGLE_BUTTON2).click();

    };
    let debounceTimer;
    const debounce = (fn, wait) => {
        return function(...args) {
            clearTimeout(debounceTimer);
            debounceTimer = setTimeout(() => fn.apply(this, args), wait);
        };
    };
    const debouncedCollapse = debounce(collapseSidebar, DEBOUNCE_WAIT);

    new MutationObserver(mutations => {
        mutations.forEach(mutation => {
            if (mutation.target.classList.contains('expanded')) {
                debouncedCollapse();
            }
        });
    }).observe(document.body, {
        subtree: true,
        attributes: true,
        attributeFilter: ['class']
    });

})();