Torn Chat Enhancer Color Usernames.

Adds usernames next to messages with colors.

目前為 2025-04-30 提交的版本,檢視 最新版本

// ==UserScript==
// @name         Torn Chat Enhancer Color Usernames.
// @namespace    http://tampermonkey.net/
// @version      1.3
// @description  Adds usernames next to messages with colors.
// @author       Mr_Awaken (Fixed Version)
// @match        *://www.torn.com/*
// @grant        none
// @run-at       document-start
// @license      All Rights Reserved
// ==/UserScript==

(function() {
    'use strict';

    // CSS to handle username styling and completely hide newMessage
    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;
        }

        /* Aggressively hide newMessage text */
        [class*="chat-box-message__box"] *:contains('newMessage') {
            font-size: 0 !important;
        }
        
        /* Hide span elements that might contain newMessage */
        [class*="chat-box-message__box"] span:empty {
            display: none !important;
        }
        
        /* Ensure text doesn't show up through font-size tricks */
        [class*="chat-box-message__box"] span:not([class]):not(:empty) {
            color: inherit !important;
            font-size: inherit !important;
        }
        
        /* Fix message spacing */
        [class*="chat-box-message__text"] {
            display: inline !important;
            margin-left: 0 !important;
        }
    `;

    // Add the custom contains selector
    function addCustomSelectors() {
        // Add a contains selector polyfill
        if (!window.jQuery) {
            // Add the :contains pseudo-selector
            document.querySelector = document.querySelector || function(){};
            const oldQSA = document.querySelectorAll;
            document.querySelectorAll = function(selector) {
                try {
                    if (selector.includes(':contains(')) {
                        // Just return an empty NodeList for this case
                        return document.createDocumentFragment().querySelectorAll("x");
                    }
                    return oldQSA.apply(this, arguments);
                } catch (e) {
                    return document.createDocumentFragment().querySelectorAll("x");
                }
            };
        }
    }

    // Insert the CSS into the page
    function addStyle() {
        const style = document.createElement('style');
        style.id = 'torn-chat-enhancer-style';
        style.textContent = css;
        document.head.appendChild(style);
    }
    
    // Directly modify and fix messages
    function fixMessages() {
        // Find all message boxes
        const messageBoxes = document.querySelectorAll('[class*="chat-box-message__box"]');
        
        messageBoxes.forEach(box => {
            // Skip already processed messages
            if (box.dataset.fixedMessages) return;
            box.dataset.fixedMessages = 'true';
            
            // First pass: clean all text nodes directly - most aggressive approach
            const walker = document.createTreeWalker(
                box,
                NodeFilter.SHOW_TEXT,
                null,
                false
            );
            
            const nodesToProcess = [];
            let textNode;
            while (textNode = walker.nextNode()) {
                nodesToProcess.push(textNode);
            }
            
            // Process and clean all text nodes
            nodesToProcess.forEach(node => {
                if (node.nodeValue && node.nodeValue.includes('newMessage')) {
                    node.nodeValue = node.nodeValue.replace(/newMessage/g, '');
                }
            });
            
            // Second pass: remove any empty text nodes or spans
            const spanElements = box.querySelectorAll('span:not([class])');
            spanElements.forEach(span => {
                if (!span.textContent.trim() || span.textContent === 'newMessage') {
                    span.remove();
                }
            });
        });
    }
    
    // Initialize and inject CSS early
    function initialize() {
        addCustomSelectors();
        
        // Check if head exists, if not wait for it
        if (document.head) {
            addStyle();
        } else {
            // Wait for head to be available
            const observer = new MutationObserver((mutations, obs) => {
                if (document.head) {
                    addStyle();
                    obs.disconnect();
                }
            });
            observer.observe(document, { childList: true, subtree: true });
        }
        
        // Set up main observer once DOM is ready
        if (document.readyState === 'loading') {
            document.addEventListener('DOMContentLoaded', setupMainObserver);
        } else {
            setupMainObserver();
        }
    }
    
    // Main observer to watch for chat changes
    function setupMainObserver() {
        // Run initial fix
        setTimeout(fixMessages, 1000);
        
        // Observe for new messages
        const observer = new MutationObserver((mutations) => {
            let hasNewMessages = false;
            
            for (const mutation of mutations) {
                if (mutation.addedNodes.length > 0) {
                    hasNewMessages = true;
                    break;
                }
            }
            
            if (hasNewMessages) {
                fixMessages();
            }
        });
        
        // Start observing the document
        observer.observe(document.body, {
            childList: true,
            subtree: true
        });
        
        // Also set up an interval to periodically check
        setInterval(fixMessages, 1000);
    }
    
    // Start the script
    initialize();
})();

QingJ © 2025

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