Ahab / Player Remover beta

// ==UserScript==
// @name Player Remover beta
// @namespace torn.com
// @include *torn.com/*
// @author Ahab [1735214]
// @version 1.9.3
// @updateURL https://openuserjs.org/meta/Ahab/Player_Remover_beta.meta.js
// @license MIT
// @require https://gist.githubusercontent.com/BrockA/2625891/raw/9c97aa67ff9c5d56be34a55ad6c18a314e5eb548/waitForKeyElements.js
// ==/UserScript==

var id = {}

if(localStorage.chatremoved === undefined){
    localStorage.chatremoved = JSON.stringify({})
    id = {}
}else if(localStorage.chatremoved.split("")[0] === '['){
    delete localStorage.chatremoved
    localStorage.chatremoved = JSON.stringify({})
}
else{
    id = JSON.parse(localStorage.chatremoved);
}

var blehTarget = $('div[id^="chatRoot"]')[0];
var blehConfig = { attributes: false, childList: true, characterData: false, subtree: true };
var bleh = new MutationObserver(function(mutations3) {
    $.each(id, function(index , value){
        $("div[class^='chat-box-message__avatar___'] a[href$='profiles.php?XID="+index+"']").parent().parent().parent().hide();
        $("a[class*='chat-box-message__sender___'][href*='/profiles.php?XID="+index+"']").parent().parent().parent().hide();
        if($("img[alt*='chat-avatar'][class*='avatar--medium'][src*='"+index+"']").length != 0 && $("img[alt*='chat-avatar'][class*='avatar--medium'][src*='"+index+"']").parent().parent().parent().parent().parent()[0].classList[0].indexOf("group-minimized-chat-box___") >= 0){
            $("img[alt*='chat-avatar'][class*='avatar--medium'][src*='"+index+"']").parent().parent().parent().parent().hide()
        }else{
            $("img[alt*='chat-avatar'][class*='avatar--medium'][src*='"+index+"']").parent().parent().parent().parent().parent().hide()
        }
    });
})
bleh.observe(blehTarget, blehConfig);

function unremove(){
    if($('div.preferences-container.m-top10').length > 0){
        var entries = JSON.parse(localStorage.chatremoved)
        $('div[class^="preferences-container"]').append("<div class='title-black top-round' id='blockedhead'>Blocked Chat Users <button class='exim' id='export' style='color:white;'>Export</button><button class='exim' id='import' style='color:white;'>Import</button><button class='exim' id='empty' style='color:white;'>Remove All</button></div>").append("<div id='blockedcont' style='background-color:#f2f2f2'></div>")
        $.each(entries, function(index, value){
            $('#blockedcont').append('<button style="width: 150px; color:#000; border-color: #ccc; border-width: thin;" id="unremovebut" class="unremovebut" data-id='+index+'>'+value+'</button>').hide();
        })
    }
    $(document).on('click', '#blockedhead', function(event) {
        if(event.target.id=="blockedhead"){
            $("#blockedcont").toggle()
        }
    })
    $(document).on('click', '#unremovebut', function(event) {
        delete id[$(event.target).attr('data-id')]
        localStorage.chatremoved = JSON.stringify(id);
        $(event.target).remove()
        $("div[class^='chat-box-message__avatar___'] a[href$='profiles.php?XID="+$(event.target).attr('data-id')+"']").parent().parent().parent().show();
        $("a[class*='chat-box-message__sender___'][href*='/profiles.php?XID="+$(event.target).attr('data-id')+"']").parent().parent().parent().show();
        $("img[alt*='chat-avatar'][class*='avatar--medium'][src*="+$(event.target).attr('data-id')+"]").parent().parent().parent().parent().show()
        $("img[alt*='chat-avatar'][class*='avatar--medium'][src*="+$(event.target).attr('data-id')+"]").parent().parent().parent().parent().parent().show()
        $( "[class^=viewport_]" ).each(function( index ){
            (this).scrollTop = (this).scrollHeight - (this).clientHeight;});
    })
    $('.exim').hover(function(){
                        $(this).css({ color: 'rgb(153, 153, 153)' });
                    }, function(){
                        $(this).css({ color: 'white' });
                    });
    $('.unremovebut').hover(function(){
        $(this).css({'background-color':'#fff'});
    }, function(){
        $(this).css({'background-color': 'transparent'});
    });
}

$(document).on('click', '#export', function(event) {
    var oldItems = JSON.stringify(JSON.parse(localStorage.getItem('chatremoved')))
    var $temp = $("<textarea>");
    $("body").append($temp);
    $temp.val(oldItems).select();
    document.execCommand("copy")
    alert("Block list copied to clipboard")
})

$(document).on('click', '#import', function(event) {
    var blocks = JSON.parse(localStorage.getItem('chatremoved'))
    var newBlocks = JSON.parse(prompt())
    Object.assign(blocks, newBlocks)
    localStorage.chatremoved = JSON.stringify(blocks)
    if(newBlocks != null){
        alert("Block list updated")
    }
})

$(document).on('click', '#empty', function(event) {
    localStorage.chatremoved = '{}'
    id = {}
    alert("All blocks removed")
    $.each($('div[class^="chat-box-message__avatar___"]').parent().parent().filter(function() { return $(this).css("display") == "none" }), function(index){
        $(this).toggle()
    })
    $.each($("a[class*='chat-box-message__sender___']").parent().parent().filter(function() { return $(this).css("display") == "none" }), function(index){
        $(this).toggle()
    })
    $.each($("img[alt*='chat-avatar']").parent().parent().parent().parent().filter(function() { return $(this).css("display") == "none" }), function(index){
        $(this).toggle()
    })
    $('div[id^="blockedcont"]').html("")
    $( "[class^=viewport_]" ).each(function( index ){
        (this).scrollTop = (this).scrollHeight - (this).clientHeight;
    });
})

function mini_hide(){
    var namemini = ""
    var observerTarget = document.body;
    var observerConfig = { attributes: false, childList: true, characterData: false, subtree: true };
    var observer = new MutationObserver(function(mutations) {
        /*$.each(id, function(index , value){
            $("div[class^='chat-box-message__avatar___'] a[href$='profiles.php?XID="+index+"']").parent().parent().parent().hide();
            $("a[class*='chat-box-message__sender___'][href*='/profiles.php?XID="+index+"']").parent().parent().parent().hide();
            $("img[alt*='chat-avatar'][class*='avatar--medium'][src*='"+index+"']").offsetParent().offsetParent().hide()
        });*/
        if($('#profile-mini-root').length > 0){
            if($('#miniremove').length === 0){
                $("#profile-mini-root > div > div > div:nth-child(5) > div.buttons-wrap > div").append('<div class="profile-button" id="miniremove"><svg id="across" width="46" height="46" stroke="#a0a0a0" style="margin-top: -4px;"><line x1="10" y1="10" x2="33" y2="33" stroke-width="3"></line><line x1="33" y1="10" x2="10" y2="33" stroke-width="3"></line></svg></div>');
                    $('#miniremove').hover(function(){
                        $('#across').css({ stroke: 'rgb(116,166,189)' });
                    }, function(){
                        $('#across').css({ stroke: 'url(#linear-gradient)' });
                    });
                $( "#miniremove" ).click(function() {
                    var userIdmini = $("#profile-mini-root > div > div > div:nth-child(5) > div.buttons-wrap > div > a").attr('href').split('ID=')[1]
                    namemini = $("div[id*='profile-mini-root'] span[class^='honor-text']")[1].innerText
                    id[userIdmini] = namemini
                    localStorage.chatremoved = JSON.stringify(id);
                    $("div[class^='chat-box-message__avatar___'] a[href$='profiles.php?XID="+userIdmini+"']").parent().parent().parent().hide();
                    $("a[class*='chat-box-message__sender___'][href*='/profiles.php?XID="+userIdmini+"']").parent().parent().parent().hide();
                    if($("img[alt*='chat-avatar'][class*='avatar--medium'][src*='"+userIdmini+"']").length != 0 && $("img[alt*='chat-avatar'][class*='avatar--medium'][src*='"+userIdmini+"']").parent().parent().parent().parent().parent()[0].classList[0].indexOf("group-minimized-chat-box___") >= 0){
                        $("img[alt*='chat-avatar'][class*='avatar--medium'][src*='"+userIdmini+"']").parent().parent().parent().parent().hide()
                    }else{
                        $("img[alt*='chat-avatar'][class*='avatar--medium'][src*='"+userIdmini+"']").parent().parent().parent().parent().parent().hide()
                    }
                });
            };
        };
        $( 'div[class^="chat-box-message__avatar___"] a:not([class]' ).on('mouseup', function(event) {
            namemini = $(event.target).text().slice(0,-2)
    });
    });
    observer.observe(observerTarget, observerConfig);
};

function shift_profile(){
    if($("div[class*='profile-mini-'] div[class*='buttons-list']").children().length > 14){
        var a = document.querySelector("#profile-mini-root > div").style.getPropertyValue("top")
        var b = document.querySelector("#profile-mini-root > div").style.getPropertyValue("left")
        var d = 0
        d = parseInt(a.split('p')[0])-35
        document.querySelector("#profile-mini-root > div").style.cssText = "top:"+d+"px; left:"+b+"";
}}

function profile_remove(){
    $('div[class^="buttons-list"]').append('<div class="profile-button" id="profileremove"><svg id="cross" width="46" height="46" stroke="#a0a0a0" style="margin-top: -1px;"><line x1="10" y1="10" x2="33" y2="33" stroke-width="3"></line><line x1="33" y1="10" x2="10" y2="33" stroke-width="3"></line></svg></div>');
    var name = $('h4[id^="skip-to-content"]').text().split("'")[0].trim()
    var userId = window.location.href.split('=')[1]
    $('#profileremove').hover(function(){
        if(localStorage.chatremoved.indexOf(userId) === -1 ){
            $('div[class^="profile-container-description"]').text('Hide '+name+' from chat?')
        }else{
            $('div[class^="profile-container-description"]').text('Unhide '+name+' from chat?')
        }
        $('#cross').css({ stroke: 'rgb(116,166,189)' });
    }, function(){
        $('#cross').css({ stroke: 'url(#linear-gradient)' });
        $('div[class^="profile-container-description"]').text('What would you like to do?')
    });
    $( "#profileremove" ).click(function() {
        id[userId] = name
        if(localStorage.chatremoved.indexOf(userId) === -1 ){
            localStorage.chatremoved = JSON.stringify(id);
            $("div[class^='chat-box-message__avatar___'] a[href$='profiles.php?XID="+userId+"']").parent().parent().parent().hide();
            $("a[class*='chat-box-message__sender___'][href*='/profiles.php?XID="+userId+"']").parent().parent().parent().hide();
            if($("img[alt*='chat-avatar'][class*='avatar--medium'][src*='"+userId+"']").length != 0 && $("img[alt*='chat-avatar'][class*='avatar--medium'][src*='"+userId+"']").parent().parent().parent().parent().parent()[0].classList[0].indexOf("group-minimized-chat-box___") >= 0){
                $("img[alt*='chat-avatar'][class*='avatar--medium'][src*='"+userId+"']").parent().parent().parent().parent().hide()
            }else{
                $("img[alt*='chat-avatar'][class*='avatar--medium'][src*='"+userId+"']").parent().parent().parent().parent().parent().hide()
            }
        }else{
            delete id[userId]
            localStorage.chatremoved = JSON.stringify(id);
            $("div[class^='chat-box-message__avatar___'] a[href$='profiles.php?XID="+userId+"']").parent().parent().parent().show();
            $("a[class*='chat-box-message__sender___'][href*='/profiles.php?XID="+userId+"']").parent().parent().parent().show();
            $("img[alt*='chat-avatar'][class*='avatar--medium'][src*="+userId+"]").parent().parent().parent().parent().show()
            $("img[alt*='chat-avatar'][class*='avatar--medium'][src*="+userId+"]").parent().parent().parent().parent().parent().show()
            $( "[class^=viewport_]" ).each(function( index ){
                (this).scrollTop = (this).scrollHeight - (this).clientHeight;
            });
        }
    });
}

waitForKeyElements("#miniremove", shift_profile);


if(window.location.href.split('/')[3].split('.')[0] === 'profiles'){
    waitForKeyElements("#profileroot > div > div > div > div:nth-child(1) > div.profile-right-wrapper.right > div.profile-buttons.profile-action > div > div.cont.bottom-round > div > div > div.buttons-wrap", profile_remove);
}

if(window.location.href.split('/')[3].split('.')[0] === 'preferences'){
    unremove()
}

mini_hide();