Gemini Codeblock: Wrap & Section Breaks

Force line wrap + add section line breaks in codeblocks on Gemini web app.

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey, Greasemonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

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

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

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

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्क्रिप्ट व्यवस्थापक एक्स्टेंशन इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्क्रिप्ट व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्टाईल व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

// ==UserScript==
// @name         Gemini Codeblock: Wrap & Section Breaks
// @namespace    http://tampermonkey.net/
// @version      1.2
// @description  Force line wrap + add section line breaks in codeblocks on Gemini web app.
// @match        https://gemini.google.com/*
// @grant        GM_addStyle
// ==/UserScript==

(function() {
    'use strict';

    // Add CSS for line wrapping in pre/code blocks (affects Gemini and any code
    GM_addStyle(`
        pre, code {
            white-space: pre-wrap !important;
            word-break: break-word !important;
        }
    `);

    // Function to apply section breaks
    function addSectionBreaksToCodeblocks() {
        document.querySelectorAll('pre > code').forEach(codeBlock => {
            // Only apply once per block
            if (codeBlock.dataset.sectionsFormatted) return;

            const lines = codeBlock.textContent.split('\n');
            const sectionRegex = /^\[[^\]]+:/;

            // Insert extra spacing after section header lines
            let newHTML = lines.map(line => {
                // If the line matches a section header (e.g. [Label: ...)
                if (sectionRegex.test(line.trim())) {
                    // Add two <br> after the section line for more vertical space
                    return `${line}<br style="line-height:1.3;"><br style="line-height:0.7;">`;
                }
                return line;
            }).join('\n');

            // Replace \n with <br> for inline HTML; preserve formatting
            codeBlock.innerHTML = newHTML.replace(/\n/g, '<br>');
            codeBlock.dataset.sectionsFormatted = "true";
        });
    }

    // Run at first load and on DOM updates
    addSectionBreaksToCodeblocks();
    const observer = new MutationObserver(addSectionBreaksToCodeblocks);
    observer.observe(document.body, { childList: true, subtree: true });
})();