lingyaobr / Blacklist de usuários no novo chat da UOL (Beta)

// ==UserScript==
// @name         Blacklist de usuários no novo chat da UOL (Beta)
// @namespace    http://www.yao-chat.ml
// @version      1.1
// @description  Mantém bloqueio de usuários que você já bloqueou antes
// @author       Ling Yao
// @match        http://batepapo.uol.com.br/beta/*
// @grant        none
// ==/UserScript==
/* jshint -W097 */
'use strict';

// Your code here...

var enableBlocks = function(){
    var blocked = $.cookie('blocked');
    if(blocked){
        blocked = JSON.parse(blocked);
        for(var i = 0; i < blocked.length; i++){
            var element = $('.usernick[title="' + blocked[i] + '"]');
            if(element.length > 0){
                angular.element(element.next('div').find('.actionBlock')).scope().toggleBlock();
            }
        }
    }
}

var setupBlackList = function(){
    var oldToggleBlockUser = angular.element($('#userList')).scope().toggleBlockUser;
    angular.element($('#userList')).scope().toggleBlockUser = function(username, toggleState){
        var blocked = $.cookie('blocked');
        if(!blocked){
            blocked = [];
        } else {
            blocked = JSON.parse(blocked);
        }
        if(toggleState){
            if(blocked.indexOf(username) == -1){
                blocked.push(username);
            }
        } else {
            var index = blocked.indexOf(username);
            if (index > -1) {
                blocked.splice(index, 1);
            }
        }
        $.cookie('blocked', JSON.stringify(blocked));
        console.log(username);
        console.log(toggleState);
        oldToggleBlockUser(username, toggleState);
    }
    
    angular.element($('#main_content')).injector().invoke(['$stateParams', 'RoomSocketFactory', function(stateParams, RoomSocketFactory) {
		var socket = RoomSocketFactory.get({tab: stateParams.node_id}).connection;
		var originalOnSuccess = socket.onSuccess;
		socket.onSuccess = function(response){
			originalOnSuccess(response);
			var message = JSON.parse(response);
			if(message.messageClass == "EnterRoomMessage"){
                var username = message.senderNick;
                var blocked = $.cookie('blocked');
                if(blocked && blocked.indexOf(username) != -1){
                    angular.element($('.usernick[title="' + username + '"]').next('div').find('.actionBlock')).scope().toggleBlock();
                }
			}
		};
	}]);
}

var blackListInterval = setInterval(function(){
	if(document.querySelector('#chatMessageSend')){
        enableBlocks();
        setupBlackList();
        clearInterval(blackListInterval);
    }
}, 100);