Zaikantos / PurgeGordonInThePast

// ==UserScript==
// @name      PurgeGordonInThePast
// @description  No more gordon messages at all. If it doesn't work on initial load, increase the setTimeout call to a higher amount.
// @version    1.00
// @match      http://*.minethings.com/miners/list_miners
// @grant        none
// @author       Zaikantos
// @require         http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js
// ==/UserScript==

var chatsDiv = document.getElementById("ChatsDiv");



(function() {
    'use strict';
    //make JQuery work
    jQuery.noConflict();

    
    //removes all gordon from the first call to the server
    //I cant seem to stop the first call since it gets made when the page is loaded, so while pex.stop() stops any further calls the first one still happens.
    //Then the options are either delete everything and use my PeriodicalExecuter to start with lastChatId = 0, or to do this since it is less visibly broken on load.
    setTimeout(function(){
        var length = chatsDiv.childNodes.length;
        for(i = length-1; i > 1; i--) {
            //ugly code to make sure it works with machina's chat script too
            //first link will always be the one to the name, even if there is a link in the message
            if (chatsDiv.childNodes[i].getElementsByTagName('a')[0].innerHTML == "gordonstretch") {
                chatsDiv.removeChild(chatsDiv.childNodes[i]);
            }
        }
    }, 500);
    //^^^^^ needs to be higher if it's not working
    
    
    //stop chat updater
    pex.stop();

    //chat updater script in page
    //see source of *.minethings.com/miners/list_miners
    var script = document.getElementById("fullcenter").childNodes[1].childNodes[7].innerHTML;

    //grab user's numerical ID, as this is used in the updater to not print your own messages
    var index = script.indexOf("chat.minerId !=")+16;
    var stringWithIndex = script.substring(index);
    var userID = stringWithIndex.substring(0, stringWithIndex.indexOf(")"));


    //We use this instead of a JQuery call in the PeriodicalExecuter


    //new PeriodicalExecuter
    //mostly identical to the one found in the source of the HTML, with an extra if statement to check for gordon and no more jquery
    var chat = new PeriodicalExecuter(function(pe) { 
        new Ajax.Request("/php/getChats.php?lastChatId="+lastChatId, {
            asynchronous:true, evalScripts:true, onComplete:function(request, json) {
                //We don't use evalJSON because greasemonkey doesn't like it
                data = jQuery.parseJSON(request.responseText);
                if (lastChatId != data.lastChatId)	
                    for (var i = 0; i < data.chats.length; i++) {		
                        chat = data.chats[i];		
                        //new line, 156710 is gordon's ID
                        if(chat.minerName != "gordonstretch") {
                            if (lastChatId == 0 || chat.minerId != userID) { //uses userID
                                if (chatIgnores.indexOf(chat.minerName) == -1)	
                                    chatsDiv.insert(chat.text);			
                                else				
                                    chatsDiv.insert("<div style=\"display:inline;font-size:6px;\">[ignored] </div>");		
                            }	
                        }
                    }
                if (data.timestamp != undefined)	
                    timestamp = data.timestamp;
                lastChatId = data.lastChatId;
                UpdateTimestamp("timestamp");
                if (atBottom) 	
                    new Effect.Tween("ChatsDiv", chatsDiv.scrollTop, chatsDiv.scrollHeight, {duration: 1.0}, "scrollTop");
            }
        }); 					
        atBottom = (lastChatId == 0 || chatsDiv.scrollTop >= (chatsDiv.scrollHeight - 510));}, 3);

    //run PeriodicalExecuter
    chat.execute();
})();