Amandra / DM colorization

// ==UserScript==
// @name         DM colorization
// @namespace    https://dreamychat.com/*
// @version      0.3
// @description  Brings large quantities of color to this site.
// @author       You
// @match        https://dreamychat.com/*
// @icon         data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==
// @require      http://code.jquery.com/jquery-latest.js
// @run-at   document-start
// @license MIT
// ==/UserScript==

var seed = 1;
function random() {
    var x = Math.sin(seed++) * 10000;
    return x - Math.floor(x);
}

function isCssLoaded(url) {
  for (let i = 0; i < document.styleSheets.length; i++) {
    const styleSheet = document.styleSheets[i];
    if (styleSheet.href === url) {
      return true;
    }
  }
  return false;
}

function HSVtoRGB(h, s, v) {
    var r1, g1, b1, i, f, p, q, t;
    if (arguments.length === 1) {
        s = h.s; v = h.v; h = h.h;
    }
    i = Math.floor(h * 6);
    f = h * 6 - i;
    p = v * (1 - s);
    q = v * (1 - f * s);
    t = v * (1 - (1 - f) * s);
    switch (i % 6) {
        case 0: r1 = v; g1 = t; b1 = p; break;
        case 1: r1 = q; g1 = v; b1 = p; break;
        case 2: r1 = p; g1 = v; b1 = t; break;
        case 3: r1 = p; g1 = q; b1 = v; break;
        case 4: r1 = t; g1 = p; b1 = v; break;
        case 5: r1 = v; g1 = p; b1 = q; break;
    }
    return [
        Math.round(r1 * 255),
        Math.round(g1 * 255),
        Math.round(b1 * 255)
    ];
}

function getName(t,dark) {
    seed = t.charCodeAt(0)*t.charCodeAt(1);
    var h = random();
    var s = random()*0.5 + 0.5;//random()*255;
    var v = 0.6;//random()*255;
    if (dark) {
        v=1;
    }
    var ar = HSVtoRGB(h,s,v);
    var r = ar[0];
    var g = ar[1];
    var b = ar[2];
    if (t == "RaeRae") {
        r = 189;
        g = 102;
        b = 231;
    }
    if (t == "cold_c4ssidy") {
        r = 255; g = 130; b = 201;
    }
    if (r > 255) {r=255;}
    if (g > 255) {g=255;}
    if (b > 255) {b=255;}
    return [r,g,b];
}

let zzz = true;

function ChangeClr(element) {
    var dark = isCssLoaded("https://dreamychat.com/css/chat-dark.css?1651288155");
    var rules = new Array();
    if (zzz) {
        zzz=false;
        for (let i = 0; i < document.styleSheets.length; i++) {

        //console.log("Here babe");
        if (document.styleSheets[i].cssRules) {
            rules = document.styleSheets[i].cssRules;
        }
        else if (document.styleSheets[i].rules) {
            rules = document.styleSheets[i].rules;
        }

        for(let j = 0; j < rules.length; j++) {
            if (String(rules[j].selectorText) == ".chat-invite.chat-self::after, .chat-invite.chat-to::after, .chat-whisper::after") {
                //document.styleSheets[0].deleteRule(rules[j]);
                rules[j].style = ".chat-invite.chat-self::after, .chat-invite.chat-to::after, .chat-whisper::after {border-width: 0 .25rem;}";
                document.styleSheets[i].cssRules[j] = rules[j];
            }
            if (String(rules[j].selectorText) == ".mention.mention-found") {
                //document.styleSheets[0].deleteRule(rules[j]);
                rules[j].style = ".mention.mention-found {}";
                document.styleSheets[i].cssRules[j] = rules[j];
            }
        }
    }
}

    if(element.classList.contains("chat-whisper")) { //Handles other people's whispers TO you

        if (!element.classList.contains("chat-self")) {
            try {
                element.classList.remove("chat-mention");
            } catch {

            }

            var s = element.getElementsByClassName("name")[0];
            var aTags = s.getElementsByTagName("span");
            var t = aTags[0].textContent;

            //console.log(t);
            var cls = getName(t,dark);
            var r = cls[0];
            var g = cls[1];
            var b = cls[2];
            var color = "rgba("+r+","+g+","+b+",.35)";
            //console.log(color);
            element.style = "background: "+color+";border: solid "+color+";border-width: 0 0.25rem;";
            //stuff[i].style.display='none';
            //stuff[i].offsetHeight; // no need to store this anywhere, the reference is enough
            //stuff[i].style.display='';
        }
    }

    //var stuff2 = element.getElementsByClassName("chat-message chat-whisper chat-self");
    if(element.classList.contains("chat-message") && element.classList.contains("chat-whisper")&&element.classList.contains("chat-self")) {
        try {
            element.classList.remove("chat-mention");
        } catch {

        }

        var s2 = element.getElementsByClassName("name")[0];
        var aTags2 = s2.getElementsByTagName("span");
        var t2 = aTags2[0].textContent;
        var cls2 = getName(t2,dark);
        var r2 = cls2[0];
        var g2 = cls2[1];
        var b2 = cls2[2];
        var color2 = "rgba("+r2+","+g2+","+b2+",1)";
        aTags2[0].style = "color: "+color2+";";
        element.style = "border: solid "+color2+";border-width: 0 0.25rem;";
    }
    var stuff3 = document.getElementsByClassName("mention-found");
    for (let i = 0; i < stuff3.length; i++) {
        aTags2 = stuff3[i];//s2.getElementsByTagName("span");
        t2 = aTags2.textContent;
        cls2 = getName(t2,dark);
        r = cls2[0];
        g = cls2[1];
        b = cls2[2];
        color = "rgba("+r+","+g+","+b+",1)";
        color2 = "rgba("+r+","+g+","+b+",0.16)";
        stuff3[i].style = "background-color: "+color2+";color: "+color+";";
    }
};

//var i = setInterval(ChangeClr,1000);

let targetNode = null;
function AddObserver() {

    if (targetNode != null) {
        return;
    }

    targetNode = document.querySelector('.messages');

    // create a new instance of MutationObserver
    let observer = new MutationObserver(function(mutationsList, observer) {
        // loop through the mutations that have occurred
        for(let mutation of mutationsList) {
            // if a new node has been added to the DOM
            if(mutation.type === 'childList' && mutation.addedNodes.length > 0) {
                // loop through the added nodes
                mutation.addedNodes.forEach(function(node) {
                    // if the added node is an li element, print "hello" to the console
                    if(node.nodeName === 'LI') {
                        //console.log('hello');
                        ChangeClr(node);
                    }
                });
            }
        }
    });
    const config = { childList: true };
observer.observe(targetNode, config);
}

function ColorNames(elements,dark) {
    for (let i = 0; i < elements.length; i++) {
        let aTags2 = elements[i].getElementsByClassName("name")[0];//s2.getElementsByTagName("span");
        let t2 = aTags2.textContent;
        let cls2 = getName(t2,dark);
        let r = cls2[0];
        let g = cls2[1];
        let b = cls2[2];
        let color = "rgba("+r+","+g+","+b+",1)";
        let color2 = "rgba("+r+","+g+","+b+",0.16)";
        aTags2.style = "background-color: "+color2+";color: "+color+";";
    }
}


let available = null;
let afk = null;
let mm = null;
function ActiveUserColors() {
    var dark = isCssLoaded("https://dreamychat.com/css/chat-dark.css?1651288155");
    if (available == null) {
        available = document.getElementById("users-available");
        afk = document.getElementById("users-afk");
        mm = document.getElementById("users-matchmaker");
    }
    if (available == null) {
        console.log("Null");
        return;
    }
    var stuff3 = available.getElementsByClassName("check-symbols");
    ColorNames(stuff3,dark);
    stuff3 = mm.getElementsByClassName("check-symbols");
    ColorNames(stuff3,dark);
    stuff3 = afk.getElementsByClassName("check-symbols");
    for (let i = 0; i < stuff3.length; i++) {
        let aTags2 = stuff3[i].getElementsByClassName("name")[0];//s2.getElementsByTagName("span");
        aTags2.style = "";
    }
}


var i = setInterval(AddObserver,250);
var j = setInterval(ActiveUserColors,500);
// configure the observer to watch for changes to the target node's child list



console.log("Dreamychat colored DMs loaded!");