Raw Source
gyakkun / Block Bad Guys on U2 (forked from joshu)

// ==UserScript==
// @name         Block Bad Guys on U2 (forked from joshu)
// @version      0.1.2
// @description  屏蔽黑名单上成员的发言
// @author       ChengChung
// @include        http*://u2.dmhy.org/*
// @grant        none
// @updateURL https://openuserjs.org/meta/gyakkun/Block_Bad_Guys_on_U2_(Forked_from_joshu).meta.js
// @license MIT
// ==/UserScript==

(function () {
	'use strict';

	var url = window.location.pathname;
	var para = window.location.search;

	const BLOCK_LIST = [43579, 38404, 44850,];
	const ENABLE_GLOBAL = true;
	const ENABLE_CHAT = true;
	const ENABLE_TORRENT_COMMENT = true;
	const ENABLE_FORUM_POST = true;
	const ENABLE_FORUM_LIST_ITEM = true;

	const SHOUTBOX_URL = /^\/shoutbox.php$/i;
	const TORRENT_DETAIL_URL = /^\/details.php$/i;
	const OFFER_URL = /^\/offers.php$/i;
	const FORUM_URL = /^\/forums.php$/i;

	const OFFER_DETAIL = /^\?.*id=\d+.*/i;
	const TOPIC_DETAIL = /^\?.*action=viewtopic.*/i;
	const TOPIC_LIST = /^\?.*action=viewforum.*/i;

	const UID_REG = /userdetails\.php\?id=(\d+)/i;

	const SHOUTBOX_ITEM_SELECTOR = "body > table > tbody > tr:nth-child(1) > td > div";
	const TORRENT_DETAIL_COMMENT_ITEM_SELECTOR = "#outer > table.main > tbody > tr > td > table > tbody > tr > td > div";
	const FORUM_POSTS_ITEM_SELECTOR = "#outer > table:nth-child(3) > tbody > tr > td > table:nth-child(2) > tbody > tr > td > div";
	const FORUM_POSTS_LIST_ITEM_SELECTOR = "#outer > table:nth-child(4) > tbody > tr";

	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.expandable-content> 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";

	const COMMENT_UID_SELECTOR = "table > tbody > tr > td:nth-child(1) > span > a";
	const TOPIC_LIST_ITEM_UID_SELECTOR = "td:nth-child(2) > span > a";

	const HINT = "BLOCKED";

	var RemoveChatFunc = function () {
		var that = $(this);
		if ($(this).find("span.expandable-content> span[style='word-break: break-all; word-wrap: break-word;'] > bdo > b").length > 0) {
			$(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) {}
			});
		}
		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 () {
				try {
					var uid = Number($(this).attr("href").match(UID_REG)[1]);
					if ($.inArray(uid, BLOCK_LIST) >= 0) {
						that.hide();
					}
				} catch (err) {}
			});
		}
		// U2娘没有身份页面呢
		if ($(this).find("span.nowrap").length > 0) {
			var uid = Number($(this).find(CHAT_UID_SELECTOR).attr("href").match(UID_REG)[1]);
			if ($.inArray(uid, BLOCK_LIST) >= 0) {
				// 移除黑名单对U2娘提问时U2娘的回答
				if ($(this).prev().find("span.nowrap").length === 0) {
					$(this).prev().remove();
				}
				$(this).remove();
			}
		}
	};

	var RemoveCommentFunc = function () {
		// 排除候选前候选后分隔栏,并排除已删号者的筛选
		if ($(this).find(COMMENT_UID_SELECTOR).length > 0) {
			var uid = Number($(this).find(COMMENT_UID_SELECTOR).attr("href").match(UID_REG)[1]);
			if ($.inArray(uid, BLOCK_LIST) >= 0) {
				$(this).next().remove();
				$(this).remove();
			}
		}
	};

	var RemoveListItemFunc = function () {
		// 排除已删号者的筛选
		if ($(this).find(TOPIC_LIST_ITEM_UID_SELECTOR).length > 0) {
			var uid = Number($(this).find(TOPIC_LIST_ITEM_UID_SELECTOR).attr("href").match(UID_REG)[1]);
			if ($.inArray(uid, BLOCK_LIST) >= 0) {
				$(this).remove();
			}
		}
	};

	if (ENABLE_GLOBAL) {
		if (ENABLE_CHAT && url.match(SHOUTBOX_URL) !== null) {
			$(SHOUTBOX_ITEM_SELECTOR).each(RemoveChatFunc);
		}
		if (ENABLE_TORRENT_COMMENT && (url.match(TORRENT_DETAIL_URL) !== null || (url.match(OFFER_URL) !== null && para.match(OFFER_DETAIL) !== null))) {
			$(TORRENT_DETAIL_COMMENT_ITEM_SELECTOR).each(RemoveCommentFunc);
		}
		if (url.match(FORUM_URL) !== null) {
			if (ENABLE_FORUM_POST && para.match(TOPIC_DETAIL) !== null) {
				$(FORUM_POSTS_ITEM_SELECTOR).each(RemoveCommentFunc);
			} else if (ENABLE_FORUM_LIST_ITEM && para.match(TOPIC_LIST) !== null) {
				$(FORUM_POSTS_LIST_ITEM_SELECTOR).each(RemoveListItemFunc);
			}
		}
	}
})();