joshu / Block Bad Guys on U2

又在无聊的事上浪费了人生 (

const BLOCK_LIST = [43579,38404,45428];
const UID_REG = /userdetails\.php\?id=(\d+)/i;
const SHOUTBOX_ITEM_SELECTOR = "body > table > tbody > tr:nth-child(1) > td > div";
const CHAT_UID_SELECTOR = "> span.nowrap > a";
//黑名单用户被引用时
const CHAT_UID_SELECTOR_2 = "> span[style='word-break: break-all; word-wrap: break-word;'] > bdo > b > a";
//在"全部展开"中被引用时
const CHAT_UID_SELECTOR_3 = "> span[style='word-break: break-all; word-wrap: break-word;'] > bdo > span.expandable-content > b > a";

var RemoveChatFunc = function() {
    var that = $(this);
    if ($(this).find("span[style='word-break: break-all; word-wrap: break-word;'] > bdo > span.expandable-content > b").length>0){
        $(this).find(CHAT_UID_SELECTOR_3).each(function(){
            var uid = Number($(this).attr("href").match(UID_REG)[1]);
            if ($.inArray(uid, BLOCK_LIST)>=0){
                that.hide();
            }
        });
    }
    if ($(this).find("span[style='word-break: break-all; word-wrap: break-word;']> bdo > b").length>0){
        $(this).find(CHAT_UID_SELECTOR_2).each(function(){
            var uid = Number($(this).attr("href").match(UID_REG)[1]);
            if ($.inArray(uid, BLOCK_LIST)>=0){
                that.hide();
            }
        });
    }
    //Original code here
    if ($(this).find("span.nowrap").length>0) {
        var uid = Number($(this).find(CHAT_UID_SELECTOR).attr("href").match(UID_REG)[1]);
        //console.log(uid);
        if ($.inArray(uid, BLOCK_LIST)>=0) {
            // 移除黑名单对U2娘提问时U2娘的回答
            if ($(this).prev().find("span.nowrap").length===0) {
                $(this).prev().remove();
            }
            $(this).remove();
        }
    }
};
$(SHOUTBOX_ITEM_SELECTOR).each(RemoveChatFunc);

今天发现失效了, 原因是文本里面有从网址文本自动转义的非userdetail的url(晕) each里面加用try catch语块可解

$(this).find(CHAT_UID_SELECTOR_3).each(function () {
    try {
        var uid = Number($(this).attr("href").match(UID_REG)[1]);
        if ($.inArray(uid, BLOCK_LIST) >= 0) {
            that.hide();
        }
    } catch (err) {}