maloomf9 / Picarto Local Chat Clear Blocker

// ==UserScript==
// @name        Picarto Local Chat Clear Blocker
// @namespace   http://www.furaffinity.net/user/Maloo
// @description Blocks chat clears locally. Requires Picarto Chat Extender. ( https://openuserjs.org/scripts/maloomf9/Picarto_Chat_Extender )
// @license     MIT
// @include     https://picarto.tv/*
// @exclude		https://picarto.tv/communities*
// @exclude		https://picarto.tv/
// @exclude		https://picarto.tv/settings*
// @exclude		https://picarto.tv/site/*
// @version     2.5
// @grant       none
// @run-at    document-start
// ==/UserScript==


function addWindowScript(fn, ident)
{
    var script = document.createElement('script');
    script.setAttribute("type", "application/javascript");
    script.setAttribute("id", ident);
    script.textContent = fn.toString().slice(fn.toString().indexOf("{") + 1,-2); //KLUUUUUUDGE.
    modifyHeadSafe(function () {document.head.appendChild(script)});
}

var headFunctions = [];
function modifyHeadSafe(toDo){
    if ((document.head) && (headFunctions.length == 0))
    {
        if (toDo != null) toDo();
    }
    else
    {
        //console.log("Pushing headFunction #" + headFunctions.length);
        headFunctions.push(toDo);
    }
}

var waitForHead = setInterval(function() {
    if (document.head)
    {
        while (headFunctions.length != 0)
        {
            //console.log("Executing headFunction #" + headFunctions.length);
            headFunctions.shift()();
        }
        clearInterval(waitForHead);
    }
},50);







addWindowScript(function () {


  var waitForHead = setInterval(function() {
     if (window.CustomFunctions)
       {
         clearInterval(waitForHead);
         document.head.appendChild(CBscript);
         console.log("ChatClearBlock Loaded.");
         CustomFunctions.callbacks.push(replaceCallbacks);

       }
  },300);

  var CBscript = document.createElement('script');
  CBscript.setAttribute("type", "application/javascript");
  CBscript.setAttribute("id", "ChatClearBlock");
  CBscript.textContent = '/*Replace caaaaallbacks*/' +

  'function replaceCallbacks() {window.socket.callbacks.ClearUserMessages[0] = function(data){'+ //Clear User Messages
  'msgClass = $("#msgs").children(".um-" + e.username);'+
  'if (msgClass.length) {'+

  'msgClass.css("background-color" , "#400000");}'+
  'addMsg(data.executionerDisplayName + " has removed " + data.username + "\'s messages")};'+

  'window.socket.callbacks.RemoveMessage[0] = function (data) {'+ //Clear Single Message
  'elem = $("#msgs").find("#msg-" + data.id);' +
  'elem.append(\'<li class="removeli">Message removed by \' + data.executionerDisplayName + \'</li>\');'+
  'elem.css("background-color" , "#400000")};'+

  'window.socket.callbacks.ClearHistory[0] = function (){'+
  '$("#msgs > li").css("background-color" , "#400000");' +
  'addMsg("Chat has been cleared (BLOCKED LOCALLY) by " + e.executionerDisplayName)'+
  '};'+
  'window.addMsg("Picarto Chat Clear Blocker 2.5 Loaded");' +
  '}';
}, "PLCCB");