Google AI Studio Width Fix

Sets style="max-width: 100%;" on some elements, unsets max-width on .chat-session-content and #account-switcher, and fixes word breaks on citations.

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Google AI Studio Width Fix
// @namespace    http://tampermonkey.net/
// @version      1.5
// @description  Sets style="max-width: 100%;" on some elements, unsets max-width on .chat-session-content and #account-switcher, and fixes word breaks on citations.
// @author       Ȼaptain Jøhn “Søap” MacTavish
// @match        https://aistudio.google.com/*
// @grant        none
// @run-at       document-idle
// @license      CC-BY-NC-SA-4.0
// ==/UserScript==

(
    function()
    {
        'use strict';

        // Selector for elements to have max-width set to 100%.
        const SetSelector = '.ng-star-inserted, .mat-mdc-tooltip-trigger.prompt-input-wrapper';

        // Selector for elements to have max-width unset.
        const UnsetSelector = '.chat-session-content, ms-prompt-box, #account-switcher';

        // Selector for elements to have word-break set to break-all.
        const WordBreakSelector = 'ms-citation';

        /**
         * Applies 'max-width: 100%' to all matching elements.
         *
         * @param {Node} RootElement The element to search within.
         */
        const ApplySetStyles = (RootElement = document) =>
        {
            const Elements = RootElement.querySelectorAll(SetSelector);
            Elements.forEach
            (
                (ElementNode) =>
                {
                    if (ElementNode.style.maxWidth !== '100%')
                    {
                        ElementNode.style.maxWidth = '100%';
                    }
                }
            );
        };

        /**
         * Unsets 'max-width' (sets to 'none') on all matching elements.
         *
         * @param {Node} RootElement The element to search within.
         */
        const ApplyUnsetStyles = (RootElement = document) =>
        {
            const Elements = RootElement.querySelectorAll(UnsetSelector);
            Elements.forEach
            (
                (ElementNode) =>
                {
                    if (ElementNode.style.maxWidth !== 'none')
                    {
                        ElementNode.style.maxWidth = 'none';
                    }
                }
            );
        };

        /**
         * Applies 'word-break: break-all' to all matching elements.
         *
         * @param {Node} RootElement The element to search within.
         */
        const ApplyWordBreakStyles = (RootElement = document) =>
        {
            const Elements = RootElement.querySelectorAll(WordBreakSelector);
            Elements.forEach
            (
                (ElementNode) =>
                {
                    if (ElementNode.style.wordBreak !== 'break-all')
                    {
                        ElementNode.style.wordBreak = 'break-all';
                    }
                }
            );
        };

        /**
         * The callback function for the MutationObserver.
         * Applies styles to newly added nodes.
         *
         * @param {Array<MutationRecord>} MutationsList The list of mutations.
         * @param {MutationObserver} ObserverInstance The observer instance.
         */
        const ObserverCallback = (MutationsList, ObserverInstance) =>
        {
            for (const Mutation of MutationsList)
            {
                if (Mutation.type === 'childList')
                {
                    Mutation.addedNodes.forEach
                    (
                        (Node) =>
                        {
                            // Ensure the node is an Element (nodeType 1)
                            if (Node.nodeType === 1)
                            {
                                // Apply styles to the new node and its descendants
                                ApplySetStyles(Node);
                                ApplyUnsetStyles(Node);
                                ApplyWordBreakStyles(Node);

                                // Check if the node itself matches the selectors
                                if (Node.matches(SetSelector))
                                {
                                    Node.style.maxWidth = '100%';
                                }
                                if (Node.matches(UnsetSelector))
                                {
                                    Node.style.maxWidth = 'none';
                                }
                                if (Node.matches(WordBreakSelector))
                                {
                                    Node.style.wordBreak = 'break-all';
                                }
                            }
                        }
                    );
                }
            }
        };

        // --- Initialization ---

        // 1. Apply styles to elements present on initial load
        ApplySetStyles();
        ApplyUnsetStyles();
        ApplyWordBreakStyles();

        // 2. Observe the DOM for dynamically added content
        const DomObserver = new MutationObserver(ObserverCallback);
        const ObserverConfig =
        {
            childList: true,
            subtree: true
        };
        DomObserver.observe(document.body, ObserverConfig);
    }
)();