Gemini Codeblock: Wrap & Section Breaks

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

Du musst eine Erweiterung wie Tampermonkey, Greasemonkey oder Violentmonkey installieren, um dieses Skript zu installieren.

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

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

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

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

Sie müssten eine Skript Manager Erweiterung installieren damit sie dieses Skript installieren können

(Ich habe schon ein Skript Manager, Lass mich es installieren!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==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 });
})();