Hide U2 blacklist user

Hide blacklist users' words

目前為 2019-01-04 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Hide U2 blacklist user
// @namespace    https://xsky123.com
// @version      1.0
// @description  Hide blacklist users' words
// @author       XSky123
// @match        *://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();
        }
    }
}


function get_blacklist(){
    blacklist = fetch_blacklist();
    localStorage.setItem('u2_blacklist', JSON.stringify(blacklist));
    localStorage.setItem('u2_blacklist_update_at', get_date());
}


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();
}
})();