maloomf9 / Picarto AdminHighlight

// ==UserScript==
// @name        Picarto AdminHighlight
// @namespace   http://www.furaffinity.net/user/Maloo
// @description Highlights anyone's messages in chat that has admin capabilities. Bugfix update 2020-04-03
// @include     https://picarto.tv/*
// @version     3.1
// @license     MIT
// @grant       none
// ==/UserScript==

function addGlobalStyle(css) {
	try {
		var elmHead, elmStyle;
		while (document.getElementsByTagName('head')[0] === null)
		{
			/*var ms;
			ms = 10;
			ms += new Date().getTime();
			while (new Date() < ms){}*/
		}
		elmHead = document.getElementsByTagName('head')[0];
		elmStyle = document.createElement('style');
		elmStyle.type = 'text/css';
		elmHead.appendChild(elmStyle);
		elmStyle.innerHTML = css;
	} catch (e) {
		if (!document.styleSheets.length) {
			document.createStyleSheet();
		}
		document.styleSheets[0].cssText += css;
	}
}



addGlobalStyle(".modMsg, .streamerMsg {border-radius: 8px !important; padding: 2px !important;}" +
               ".modMsg .theMsg, .streamerMsg .theMsg {color: #FFFFFF !important;}" +
".modMsg {background: #5d85d0 !important;}" + //.msgModContent
".streamerMsg {background: #ca4b4b !important;"); //.msgAdminContent

var tarEl = document.querySelector("#msgs");

var updateHighlights = function(mutList)
{
    var atBottom = document.querySelector(".scrollwrapperchat").scrollTop - (document.querySelector(".scrollwrapperchat").scrollTopMax - 15);
    var count = 0;
    //console.log("atBottom: " + atBottom);
    mutList.forEach(function(mutati) {
        mutati.addedNodes.forEach(function(node) {
            if (node.querySelector("[title=\"Streamer\"]"))
            {
                node.querySelector(".msgContent:not(.modMsg):not(.streamerMsg)").classList.add("streamerMsg");
                count++;
            }
            else if (node.querySelector("[title=\"Moderator\"]"))
            {
                node.querySelector(".msgContent:not(.modMsg):not(.streamerMsg)").classList.add("modMsg");
                count++;
            }
        });
    });
    if ((atBottom > 0) && (count > 0))
    {
        //console.log("I should be trying to scroll down now");
        setTimeout(function(){
            document.querySelector(".scrollwrapperchat").scrollTop = document.querySelector(".scrollwrapperchat").scrollTopMax;
            document.querySelector("#chatNewMessageNotification").click();
        }, 250);
    }
}

var mutObserver = new MutationObserver(updateHighlights);

mutObserver.observe(tarEl, {childList: true});