Hide U2 blacklist user

Hide blacklist users' words

目前为 2019-01-04 提交的版本。查看 最新版本

// ==UserScript==
// @name         Hide U2 blacklist user
// @namespace    https://xsky123.com
// @version      1.2
// @description  Hide blacklist users' words
// @author       XSky123
// @match        https://u2.dmhy.org
// @match        https://u2.dmhy.org/*
// @match        http://u2.dmhy.org
// @match        http://u2.dmhy.org/*
// @noframes
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

// GLOBAL SETTINGS
// 1. Disable async in ajax
    $.ajaxSetup({
        async : false
    });

// 2. Set empty blacklist
    var blacklist = [];


    function get_date() {
        var now = new Date();
        var year = now.getFullYear();
        var month = now.getMonth() + 1;
        var day = now.getDate();
        var hour = now.getHours();
        var minute = now.getMinutes();
        var second = now.getSeconds();
        return `${year}-${month < 10 ? "0"+month : month}-${day < 10 ? "0"+day : day} ${hour < 10 ? "0"+hour : hour}:${minute < 10 ? "0"+hour : hour}:${second < 10 ? "0"+second : second}`;
    }


// Step 1: Fetch blacklist from localStorage
    function init_blacklist(){
        if(!localStorage.getItem('u2_blacklist')){ // not storaged
            get_blacklist();
        }else{
            // check refresh
            if(localStorage.getItem('u2_blacklist_update_at')){
                var now = get_date();
                var last_update_at = new Date(localStorage.getItem('u2_blacklist_update_at'));
                if(now - last_update_at > 24 * 3600 * 1000){ // if over 1 day fetch new
                    get_blacklist();
                }else{                                       // else use local data
                    blacklist = JSON.parse(localStorage.getItem('u2_blacklist'));
                }
            }else{
                get_blacklist();
            }
        }
        if(window.location.href.search("friends") !== -1){ // if visit friends page, update.
            get_blacklist();
        }
    }


    function get_blacklist(){
        blacklist = fetch_blacklist();
        localStorage.setItem('u2_blacklist', JSON.stringify(blacklist));
        localStorage.setItem('u2_blacklist_update_at', get_date());
        console.log("Blacklist has refreshed");
    }


    function fetch_blacklist(){
        var black_list = [];
        var friend_page = "";
        var all_blocked = [];
        var finish_flag = 0;
        $.get("https://u2.dmhy.org/friends.php", function(data, status){
            // alert("Data: " + data + "\nStatus: " + status);
            if(status === "success"){
                friend_page = data;
                all_blocked = friend_page.match(/type=block.*?ltr'>(.*?)<\/bdo/g);
                all_blocked.forEach(function(each){
                    black_list.push(each.match(/targetid=(.*?)"/)[1]);
                });
                finish_flag = 1;
            }else{
                throw new Error("Can not access friends page.");
            }
        });
        return black_list;

    }




// Step 2.1: Hide MotherFxxker's words in shoutbox
    function hide_shoutbox_dirty_words(){
        var shoutbox = document.getElementsByTagName('iframe')[0];
        if(shoutbox.contentWindow.document.getElementsByTagName("head").innerText == "")
            ;
        else{
            var shoutrow = shoutbox.contentWindow.document.getElementsByClassName("shoutrow");
            var shoutrows = shoutrow[0].getElementsByTagName("div");
            var uid;
            var i;
            var count = 0;
            for(i=0;i<shoutrows.length;i++)
            {
                var each = shoutrows[i];

                uid = each.innerHTML.match(/sbat\((.*?)\)/);
                if(!uid)
                    continue;

                if(blacklist.indexOf(uid[1]) !== -1){ // find blocked user
                    each.getElementsByTagName("bdo")[1].innerHTML = "<i>黑名单用户发言,已屏蔽</i>";
                    count++;
                }
            }
            console.log("Blocked " + count + " messages.")
        }
    }


    window.addEventListener("load",init,false);
    init_blacklist();
    function init(){
        document.getElementsByTagName('iframe')[0].onload = function () {
            hide_shoutbox_dirty_words()
        };
        hide_shoutbox_dirty_words();
    }
})();