Chatable Google AI Studio 聊得下去的 Gemini 开发者模式

Auto collapse sidebars in Google AI Studio. 原网页总是莫名弹出左右栏,非常反人类。Trim excess top and bottom margins. 去除上下多余空白,释放阅读空间。The script will automatically activate the grounding(google search) feature for new chats. 自动点开google搜索。Adjust fonts for better readability. 调整字体、大小、行高、线条粗细、使用像素级别抗锯齿等等(在代码末尾的css里修改)。

目前为 2025-03-01 提交的版本。查看 最新版本

// ==UserScript==
// @name         Chatable Google AI Studio 聊得下去的 Gemini 开发者模式
// @namespace    http://tampermonkey.net/
// @version      2.0
// @description  Auto collapse sidebars in Google AI Studio. 原网页总是莫名弹出左右栏,非常反人类。Trim excess top and bottom margins. 去除上下多余空白,释放阅读空间。The script will automatically activate the grounding(google search) feature for new chats. 自动点开google搜索。Adjust fonts for better readability. 调整字体、大小、行高、线条粗细、使用像素级别抗锯齿等等(在代码末尾的css里修改)。
// @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']
    });

//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

    const style = document.createElement('style');
    style.textContent = `
        body > app-root > div > div > div > select-header {
            height: 50.25px;
        }
        body > app-root > div > div > div > div > span > ms-prompt-switcher > ms-chunk-editor > section > div > div > ms-system-instructions{
            padding-top : 0 !important;
        }
        body > app-root > div > div > div > div > span > ms-prompt-switcher > ms-chunk-editor > section > div > div > ms-system-instructions > div{
            margin : 0!important;
            /*padding: 0!important;*/
        }
        body > app-root > div > div > div > div > span > ms-prompt-switcher > ms-chunk-editor > section > footer{
            margin-bottom : 0 !important;
            padding-bottom: 0!important;
        }


        body:not(.dark-theme) ms-cmark-node p {
            font-size: 15.5px !important;
            line-height: 1.6 !important;
            font-family:  "Roboto", "Open Sans", "Source Han Sans", system-ui, -apple-system, "PingFang SC", "Microsoft YaHei", sans-serif !important;
            /*font-weight: 300 !important;*/
            -webkit-font-smoothing: antialiased !important;
            -moz-osx-font-smoothing: grayscale !important;
            text-rendering: optimizeLegibility !important;
        }
        body:not(.dark-theme) ms-cmark-node .inline-code {
            font-size: 15.5px !important;
            letter-spacing: 0.1em !important; /* 略微增加字间距 */
            font-family: 'SF Mono', 'JetBrains Mono', Menlo, Consolas, monospace !important;
        }
        body:not(.dark-theme) ms-cmark-node pre code {
            font-size: 14.5px !important;
            font-family: Consolas, 'SF Mono', 'Roboto Mono', 'Cascadia Code', Menlo,  monospace !important;
        }

    `;
    document.head.appendChild(style);

})();