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 για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Userscripts για να εγκαταστήσετε αυτόν τον κώδικα.

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

Θα χρειαστεί να εγκαταστήσετε μια επέκταση διαχείρισης κώδικα χρήστη για να εγκαταστήσετε αυτόν τον κώδικα.

(Έχω ήδη έναν διαχειριστή κώδικα χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

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.

(Έχω ήδη έναν διαχειριστή στυλ χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

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