MWI Colorful Chat

Please Don't use this...

目前為 2025-07-08 提交的版本,檢視 最新版本

// ==UserScript==
// @name         MWI Colorful Chat
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  Please Don't use this...
// @author       SilkyPanda
// @license      CC-BY-NC-SA-4.0
// @match        https://www.milkywayidle.com/*
// @match        https://test.milkywayidle.com/*
// @grant        none
// @run-at       document-idle
// ==/UserScript==

(function() {
    'use strict';
    const CHAT_MESSAGE_SELECTOR = '[class^="ChatMessage_chatMessage"]';

    const BRIGHT_COLORS = [
        '#FF00FF', '#00FFFF', '#00FF00', '#FFFF00', '#FF7F50', '#FF4500',
        '#FF1493', '#ADFF2F', '#7FFF00', '#00FA9A', '#00BFFF', '#1E90FF',
        '#DA70D6', '#BA55D3', '#9400D3', '#FFD700', '#F0E68C', '#E6E6FA',
        '#FF69B4', '#FFA500', '#FF6347', '#D2691E', '#BDB76B', '#48D1CC',
        '#AFEEEE', '#5F9EA0', '#FF8C00', '#FF00A1', '#A1FF00', '#00A1FF',
        '#32CD32', '#F5DEB3'
    ];

    function colorizeNode(targetNode) {
        if (!targetNode) return;

        const fragment = document.createDocumentFragment();

        targetNode.childNodes.forEach(childNode => {
            if (childNode.nodeName === 'A') {
                const link = document.createElement('a');
                link.href = childNode.href;
                link.target = '_blank';
                link.rel = 'noreferrer noopener nofollow';
                for (const char of childNode.textContent) {
                    const letterSpan = document.createElement('span');
                    letterSpan.textContent = char;
                    if (char.trim() !== '') {
                        letterSpan.style.color = BRIGHT_COLORS[Math.floor(Math.random() * BRIGHT_COLORS.length)];
                    }
                    link.appendChild(letterSpan);
                }
                fragment.appendChild(link);
            } else if (childNode.nodeType === Node.TEXT_NODE) {
                for (const char of childNode.textContent) {
                    const letterSpan = document.createElement('span');
                    letterSpan.textContent = char;
                    if (char.trim() !== '') {
                        letterSpan.style.color = BRIGHT_COLORS[Math.floor(Math.random() * BRIGHT_COLORS.length)];
                    }
                    fragment.appendChild(letterSpan);
                }
            } else { // Append any other nodes as-is
                fragment.appendChild(childNode.cloneNode(true));
            }
        });

        targetNode.innerHTML = '';
        targetNode.appendChild(fragment);
    }

    function processChatMessage(messageElement) {
        if (messageElement.dataset.rainbowified) return;
        messageElement.dataset.rainbowified = 'true';

        const playerNameSpan = messageElement.querySelector('div[class^="CharacterName_name"] > span');
        colorizeNode(playerNameSpan);

        const messageTextSpan = messageElement.querySelector(':scope > span:last-of-type');
        colorizeNode(messageTextSpan);
    }


    const observer = new MutationObserver((mutationsList) => {
        for (const mutation of mutationsList) {
            mutation.addedNodes.forEach(node => {
                if (node.nodeType === Node.ELEMENT_NODE) {
                    if (node.matches(CHAT_MESSAGE_SELECTOR)) {
                        processChatMessage(node);
                    }
                    node.querySelectorAll(CHAT_MESSAGE_SELECTOR).forEach(processChatMessage);
                }
            });
        }
    });

    function initialize() {
        document.querySelectorAll(CHAT_MESSAGE_SELECTOR).forEach(processChatMessage);
        observer.observe(document.body, { childList: true, subtree: true });
        console.log('MWI Ultimate Rainbow Chat is now active.');
    }

    console.log('MWI Ultimate Rainbow Chat v4.0 loaded!');
    setTimeout(initialize, 3000);

})();