Google AI Studio Width Fix

Sets style="max-width: 100%;" on some elements and unsets max-width on .chat-session-content.

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Google AI Studio Width Fix
// @namespace    http://tampermonkey.net/
// @version      1.3
// @description  Sets style="max-width: 100%;" on some elements and unsets max-width on .chat-session-content.
// @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';

        /**
         * 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
            (
                (element) =>
                {
                    if (element.style.maxWidth !== '100%')
                    {
                        element.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
        ) =>
        {
            // The [_ngcontent-ng-{anything}] is a dynamic attribute, so we select by the static class.
            const elements = rootElement.querySelectorAll(unsetSelector);
            elements.forEach
            (
                (element) =>
                {
                    if (element.style.maxWidth !== 'none')
                    {
                        element.style.maxWidth = 'none';
                    }
                }
            );
        };

        /**
         * The callback function for the MutationObserver.
         * Applies styles to newly added nodes.
         *
         * @param {Array<MutationRecord>} mutationsList The list of mutations.
         * @param {MutationObserver} observer The observer instance.
         */
        const observerCallback =
        (
            mutationsList,
            observer
        ) =>
        {
            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);

                                // Check if the node itself matches the selectors
                                if (node.matches(setSelector))
                                {
                                    node.style.maxWidth = '100%';
                                }
                                if (node.matches(unsetSelector))
                                {
                                    node.style.maxWidth = 'none';
                                }
                            }
                        }
                    );
                }
            }
        };

        // --- Initialization ---

        // 1. Apply styles to elements present on initial load
        applySetStyles();
        applyUnsetStyles();

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