Torn Chat Enhancer 1.0 Color Usernames

Adds usernames next to messages and makes it more colourful.

当前为 2025-04-27 提交的版本,查看 最新版本

// ==UserScript==
// @name         Torn Chat  Enhancer 1.0 Color Usernames
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  Adds usernames next to messages and makes it more colourful.
// @author       Mr_Awaken 
// @match        *://www.torn.com/*
// @grant        none
// @run-at       document-idle
// @license      All Rights Reserved 
// ==/UserScript==

(function() {
    'use strict';

    // CSS to handle username styling and fix visibility
    const css = `
        /* Reveal the hidden "Name:" link in private chat bubbles */
        [class*="chat-box-message__box"] a[class*="chat-box-message__sender___"] {
            display: inline !important;
            visibility: visible !important;
            font-weight: bold !important;
            margin-right: 5px !important;
            text-decoration: none !important;
        }

        /* Your messages: bright green */
        [class*="chat-box-message__box--self"] a[class*="chat-box-message__sender___"] {
            color: #00C853 !important;
        }

        /* Their messages: deep royal blue for max contrast */
        [class*="chat-box-message__box"]:not([class*="chat-box-message__box--self"]) 
        a[class*="chat-box-message__sender___"] {
            color: #2962FF !important;
        }

        /* Fix for "newMessage" text - hide any text node directly before the username link */
        [class*="chat-box-message__box"] a[class*="chat-box-message__sender___"]::before {
            content: "" !important;
            display: none !important;
        }

        /* Hide any content directly after username until the first space to prevent emoji artifacts */
        [class*="chat-box-message__box"] a[class*="chat-box-message__sender___"] + span:not([class]) {
            display: inline !important;
        }
        
        /* Use CSS to hide emoji patterns after usernames */
        [class*="chat-box-message__box"] span:empty + [class*="chat-box-message__text"] {
            text-indent: 0 !important; 
        }

        /* Hide any "newMessage" text that might be inserted as a separate element */
        [class*="chat-box-message__newmessage"],
        [class*="newMessage"],
        [class*="new-message"] {
            display: none !important;
        }
    `;

    // Insert the CSS into the page
    const style = document.createElement('style');
    style.textContent = css;
    document.head.appendChild(style);

    // Function to handle dynamic content and fix any "newMessage" text
    function fixNewMessageText() {
        // Find all message sender elements
        const senderElements = document.querySelectorAll('[class*="chat-box-message__sender___"]');
        
        senderElements.forEach(sender => {
            // Get the parent message box element
            const messageBox = sender.closest('[class*="chat-box-message__box"]');
            
            if (messageBox) {
                // Check all text nodes in the message box
                const walker = document.createTreeWalker(
                    messageBox,
                    NodeFilter.SHOW_TEXT,
                    null,
                    false
                );
                
                let node;
                while (node = walker.nextNode()) {
                    // If the text contains "newMessage", remove it
                    if (node.nodeValue) {
                        // Remove "newMessage" text
                        if (node.nodeValue.includes('newMessage')) {
                            node.nodeValue = node.nodeValue.replace(/newMessage/g, '');
                        }
                        
                        // Remove emoji prefixes like ":hi" before usernames
                        if (node.nodeValue.match(/:\w+\s*$/)) {
                            node.nodeValue = '';
                        }
                        
                        // Check for emoji pattern at start of message (username:emoji)
                        if (node.nodeValue.match(/^[^:]+:(\w+)/)) {
                            node.nodeValue = node.nodeValue.replace(/^([^:]+):(\w+)/, '$1');
                        }
                    }
                }
                
                // Also clean up any direct text content that might have emojis
                // Get the text content before the sender link
                const childNodes = Array.from(messageBox.childNodes);
                for (let i = 0; i < childNodes.length; i++) {
                    const node = childNodes[i];
                    if (node.nodeType === Node.TEXT_NODE) {
                        // If there's text right before or after the username that contains emoji patterns
                        if (node.nodeValue && node.nodeValue.match(/:\w+/)) {
                            node.nodeValue = node.nodeValue.replace(/:\w+/g, '');
                        }
                    }
                }
            }
        });
    }

    // Initial fix
    fixNewMessageText();

    // Setup mutation observer to catch dynamically added content
    const observer = new MutationObserver((mutations) => {
        let shouldFix = false;
        
        mutations.forEach(mutation => {
            // Check if nodes were added
            if (mutation.addedNodes.length > 0) {
                for (let i = 0; i < mutation.addedNodes.length; i++) {
                    const node = mutation.addedNodes[i];
                    
                    // Check if this is a chat message or contains chat elements
                    if (node.nodeType === 1 && (
                        node.classList && (
                            node.classList.toString().includes('chat-box-message') ||
                            node.querySelector('[class*="chat-box-message"]')
                        )
                    )) {
                        shouldFix = true;
                        break;
                    }
                }
            }
        });
        
        if (shouldFix) {
            fixNewMessageText();
        }
    });

    // Start observing the document with the configured observer
    observer.observe(document.body, {
        childList: true,
        subtree: true
    });

    // Also fix on any chat container visibility changes
    function setupVisibilityCheck() {
        const chatContainers = document.querySelectorAll('[class*="chat-box__"]');
        
        chatContainers.forEach(container => {
            // Use IntersectionObserver to detect when chat becomes visible
            const visibilityObserver = new IntersectionObserver((entries) => {
                entries.forEach(entry => {
                    if (entry.isIntersecting) {
                        fixNewMessageText();
                    }
                });
            });
            
            visibilityObserver.observe(container);
        });
    }

    // Setup initial visibility checking
    setTimeout(setupVisibilityCheck, 2000);

    // Re-check periodically for any new chat containers
    setInterval(() => {
        setupVisibilityCheck();
        fixNewMessageText();
    }, 5000);
})();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址