Gemini JSON Wrapper – Highlight-Safe,

Unwraps Gemini JSON, soft-fixes \\n, preserves highlight

Dovrai installare un'estensione come Tampermonkey, Greasemonkey o Violentmonkey per installare questo script.

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

Dovrai installare un'estensione come Tampermonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Userscripts per installare questo script.

Dovrai installare un'estensione come ad esempio Tampermonkey per installare questo script.

Dovrai installare un gestore di script utente per installare questo script.

(Ho già un gestore di script utente, lasciamelo installare!)

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

(Ho già un gestore di stile utente, lasciamelo installare!)

// ==UserScript==
// @name         Gemini JSON Wrapper – Highlight-Safe, 
// @namespace    http://tampermonkey.net/
// @version      3.0
// @description  Unwraps Gemini JSON, soft-fixes \\n, preserves highlight
// @author       copypaste
// @match        *://gemini.google.com/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    const CONFIG = {
        wrapStyles: {
            whiteSpace: 'pre-wrap',
            wordBreak: 'break-word',
            overflowWrap: 'break-word',
            overflowX: 'auto',
            maxWidth: '100%',
            fontFamily: 'monospace',
            fontSize: '13px',
            lineHeight: '1.4'
        },
        targetSelectors: [
            '[data-text-generation-response] pre',
            '[data-text-generation-response] code',
            '.text-generation-response pre',
            '.text-generation-response code',
            '.response-output pre',
            '.response-output code',
            'pre',
            'code'
        ],
        checkInterval: 2000,
        minJsonLength: 100,
        dataAttribute: 'data-json-wrapped'
    };

    function looksLikeJSON(str) {
        const trimmed = str.trim();
        if (!trimmed.startsWith('{') && !trimmed.startsWith('[')) return false;
        try {
            JSON.parse(trimmed);
            return true;
        } catch {
            return false;
        }
    }

    function softUnescape(str) {
        return str.replace(/\\n/g, '\n').replace(/\\t/g, '\t');
    }

    function processCodeElements() {
        let changes = 0;
        CONFIG.targetSelectors.forEach(selector => {
            document.querySelectorAll(selector).forEach(el => {
                if (el.hasAttribute(CONFIG.dataAttribute)) return;

                const hasSpans = el.innerHTML.includes('<span');
                if (!hasSpans) {
                    const txt = el.textContent.trim();
                    if (txt.length >= CONFIG.minJsonLength && looksLikeJSON(txt)) {
                        el.textContent = softUnescape(txt);
                        changes++;
                    }
                }

                Object.assign(el.style, CONFIG.wrapStyles);
                el.setAttribute(CONFIG.dataAttribute, 'true');
            });
        });

        if (changes > 0) console.debug(`[JSON Wrapper] Updated ${changes} elements`);
    }

    function debounce(func, delay) {
        let timeoutId;
        return function(...args) {
            clearTimeout(timeoutId);
            timeoutId = setTimeout(() => func.apply(this, args), delay);
        };
    }

    const debouncedProcess = debounce(processCodeElements, 500);

    function initObserver() {
        const observer = new MutationObserver((mutations) => {
            let shouldProcess = false;
            for (const mutation of mutations) {
                if (
                    mutation.type === 'childList' && mutation.addedNodes.length > 0 ||
                    (mutation.type === 'attributes' &&
                     CONFIG.targetSelectors.some(sel => sel.includes(mutation.attributeName)))
                ) {
                    shouldProcess = true;
                    break;
                }
            }
            if (shouldProcess) debouncedProcess();
        });

        const containers = document.querySelectorAll('.response-output, [data-text-generation-response]');
        if (containers.length > 0) {
            containers.forEach(container => {
                observer.observe(container, {
                    childList: true,
                    subtree: true,
                    attributes: true,
                    attributeFilter: ['class', 'data-text-generation-response']
                });
            });
        } else {
            observer.observe(document.body, {
                childList: true,
                subtree: true
            });
        }
    }

    function init() {
        processCodeElements(); // Initial run
        initObserver();        // Watch for dynamic loads

        setInterval(() => {
            debouncedProcess(); // Lazy interval refresh in case observer misses something
        }, CONFIG.checkInterval);
    }

    if (document.readyState === 'loading') {
        document.addEventListener('DOMContentLoaded', init);
    } else {
        init();
    }

})();