Italic Nomi

Makes * text italic

// ==UserScript==
// @name         Italic Nomi
// @namespace    https://ghezzo.net/
// @version      0.2
// @description  Makes * text italic
// @author       Ghezzo
// @match        https://beta.nomi.ai/nomis*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=nomi.ai
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    // Custom color for italicized text
    const italicColor = '#dbd7d7'; // Change this to any color you like

    // Function to process text nodes
    function processTextNode(node) {
        // Regular expression to match *text* pattern only when there's no space between asterisks and the text
        const italicPattern = /\*(\S(.*?\S)?)\*/g;

        // Create a container to hold the new HTML with italics and color
        const newHTML = node.textContent.replace(italicPattern, (match, p1) => `*<em style="color: ${italicColor};">${p1}</em>*`);

        // Only replace if there's a match
        if (newHTML !== node.textContent) {
            // Create a span element and set it as HTML
            const span = document.createElement('span');
            span.innerHTML = newHTML;

            // Replace the original text node with the new HTML span
            node.replaceWith(span);
        }
    }

    // Recursive function to walk through all text nodes
    function walk(node) {
        let child, next;

        switch (node.nodeType) {
            case 1: // Element node
            case 9: // Document node
            case 11: // Document fragment node
                child = node.firstChild;
                while (child) {
                    next = child.nextSibling;
                    walk(child);
                    child = next;
                }
                break;

            case 3: // Text node
                processTextNode(node);
                break;
        }
    }

    // Initial run on the whole body
    walk(document.body);

    // Set up MutationObserver to watch for added nodes
    const observer = new MutationObserver(mutations => {
        for (const mutation of mutations) {
            if (mutation.type === 'childList') {
                mutation.addedNodes.forEach(node => {
                    walk(node);
                });
            }
        }
    });

    // Observe the document body for changes
    observer.observe(document.body, { childList: true, subtree: true });

})();