Greasy Fork

Greasy Fork is available in English.

哔哩哔哩 - 屏蔽指定内容

实现按用户名或直接以标题中的关键字进行屏蔽

当前为 2020-03-26 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         哔哩哔哩 - 屏蔽指定内容
// @namespace    http://greasyfork.icu/zh-CN/users/193133-pana
// @homepage     https://www.sailboatweb.com
// @version      1.2.0
// @description  实现按用户名或直接以标题中的关键字进行屏蔽
// @author       pana
// @include      http*://www.bilibili.com/*
// @include      http*://search.bilibili.com/*
// @require      https://cdn.bootcss.com/jquery/2.2.4/jquery.min.js
// @require      https://cdnjs.cloudflare.com/ajax/libs/arrive/2.4.1/arrive.min.js
// @grant        GM_getValue
// @grant        GM_setValue
// @grant        GM_registerMenuCommand
// @grant        GM_info
// @run-at       document-start
// @note         ver.1.2.0 添加屏蔽评论的功能
// @note         ver.1.1.2 调整屏蔽按钮的位置到右下角; 尝试处理脚本偶尔会失效的问题
// @note         ver.1.1.1 修复搜索页面以关键字屏蔽无效的问题
// @note         ver.1.1.0 匹配视频播放页面; 优化代码
// ==/UserScript==

(function() {
	'use strict';
	const HEAD_TITLE = 'bilibili_屏蔽设置 ver.' + GM_info.script.version;
	const OLD_URL = location.href;
	const CHECKBOX_VALUE = {
		START_ARRAY: ['startInput', '启用屏蔽功能'],
		USERNAME_ARRAY: ['usernameInput', '--> 按up主名字进行屏蔽'],
		KEYWORD_ARRAY: ['keywordInput', '--> 按关键字进行屏蔽(针对视频标题或评论)'],
		COMMENT_ARRAY: ['commentInput', '--> 允许屏蔽评论'],
		SAVE_CLOSE_ARRAY: ['saveCloseInput', '保存的同时关闭设置框'],
	};
	var bilibili_config = {
		functionEnable: true,
		usernameEnable: true,
		keywordEnable: true,
		commentEnable: false,
		saveCloseEnable: true,
		usernameArray: [],
		keywordArray: []
	};

	function string_2_Array(text_string) {
		let temp_array = text_string.split(',');
		let return_array = [];
		for (let i = 0, l = temp_array.length; i < l; i++) {
			for (let j = i + 1; j < l; j++) {
				if (temp_array[i] === temp_array[j]) {
					++i;
					j = i
				}
			}
			return_array.push(temp_array[i])
		}
		return return_array
	}
	function array_2_String(text_array) {
		return text_array.join(',')
	}
	function expand_Wrap(id_value) {
		if (document.getElementById(id_value).style.display === 'block') {
			document.getElementById(id_value).style.display = 'none'
		} else {
			document.getElementById(id_value).style.display = 'block'
		}
	}
	function insert_Checkbox(ul_node, li_array, check_enable_value) {
		let temp_li = document.createElement('li');
		temp_li.innerHTML = '<label style="vertical-align: middle;"><input id="' + li_array[0] + '" type="checkbox" style="margin-right: 5px; margin-bottom: 6px; cursor: pointer;"' + (check_enable_value === true ? 'checked' : '') + '>' + li_array[1] + '</label>';
		ul_node.appendChild(temp_li)
	}
	function decide_Keyword(headline, is_comment = false) {
		if (bilibili_config.functionEnable) {
			if ((!is_comment) || (is_comment && bilibili_config.commentEnable)) {
				if (bilibili_config.keywordEnable) {
					for (let i = 0; i < bilibili_config.keywordArray.length; i++) {
						if ((bilibili_config.keywordArray[i]) && (headline) && (headline.indexOf(bilibili_config.keywordArray[i]) !== -1)) {
							return true
						}
					}
				}
			}
		}
		return false
	}
	function decide_Username(username, is_comment = false) {
		if (bilibili_config.functionEnable) {
			if ((!is_comment) || (is_comment && bilibili_config.commentEnable)) {
				if (username && bilibili_config.usernameEnable) {
					return bilibili_config.usernameArray.includes(username)
				}
			}
		}
		return false
	}
	function hide_Event() {
		if (OLD_URL.indexOf('www.bilibili.com') !== -1) {
			let common_card = $('.video-card-common');
			$.each(common_card, function(_index, item) {
				if (decide_Username($(item).find('a.up:first').text().replace(/\s+$/i, '')) || decide_Username($(item).find('a.ex-up:first').text().replace(/\s+$/i, ''))) {
					$(item).hide()
				} else if (decide_Keyword($(item).find('a.title:first').text()) || decide_Keyword($(item).find('p.ex-title:first').text())) {
					$(item).hide()
				} else {
					$(item).css('display', '')
				}
			});
			let reco_card = $('.video-card-reco');
			$.each(reco_card, function(_index, item) {
				if (decide_Username($(item).find('p.up:first').text().replace(/\s+$/i, ''))) {
					$(item).hide()
				} else if (decide_Keyword($(item).find('p.title:first').text())) {
					$(item).hide()
				} else {
					$(item).css('display', '')
				}
			});
			let rank_wrap = $('.rank-wrap');
			$.each(rank_wrap, function(_index, item) {
				if (decide_Username($(item).find('span.name:first').text())) {
					$(item).hide()
				} else if (decide_Keyword($(item).find('p.f-title:first').text())) {
					$(item).hide()
				} else {
					$(item).css('display', '')
				}
			});
			let spread_module = $('.spread-module');
			$.each(spread_module, function(_index, item) {
				if (decide_Keyword($(item).find('p.t:first').text())) {
					$(item).hide()
				} else {
					$(item).css('display', '')
				}
			});
			let groom_module = $('.groom-module');
			$.each(groom_module, function(_index, item) {
				if (decide_Username($(item).find('p.author:first').text().replace(/^up主:|\s+$/i, ''))) {
					$(item).hide()
				} else if (decide_Keyword($(item).find('p.title:first').text())) {
					$(item).hide()
				} else {
					$(item).css('display', '')
				}
			});
			let rank_item = $('.rank-item');
			$.each(rank_item, function(_index, item) {
				if (decide_Keyword($(item).find('p.ri-title:first').text())) {
					$(item).hide()
				} else {
					$(item).css('display', '')
				}
			});
			let recent_hot = $('div#recent_hot li');
			$.each(recent_hot, function(_index, item) {
				if (decide_Keyword($(item).attr('title'))) {
					$(item).hide()
				} else {
					$(item).css('display', '')
				}
			});
			let vd_list = $('ul.vd-list li');
			$.each(vd_list, function(_index, item) {
				if (decide_Username($(item).find('a.v-author:first').text())) {
					$(item).hide()
				} else if (decide_Keyword($(item).find('a.title:first').text())) {
					$(item).hide()
				} else {
					$(item).css('display', '')
				}
			});
			let video_page_card = $('.video-page-card');
			$.each(video_page_card, function(_itenx, item) {
				if (decide_Username($(item).find('div.up:first').text())) {
					$(item).hide()
				} else if (decide_Keyword($(item).find('a.title:first').text())) {
					$(item).hide()
				} else {
					$(item).css('display', '')
				}
			});
			if (OLD_URL.indexOf('www.bilibili.com/video/') !== -1) {
				let con_list = $('.comment-list .list-item');
				$.each(con_list, function(_index, item) {
					if (decide_Username($(item).find('.con > .user a.name:first').text(), true)) {
						$(item).hide()
					} else if (decide_Keyword($(item).find('.con > p.text:first').text(), true)) {
						$(item).hide()
					} else {
						$(item).css('display', 'block')
					}
				});
				let reply_con = $('.comment-list .reply-item');
				$.each(reply_con, function(_index, item) {
					if (decide_Username($(item).find('.reply-con .user a.name:first').text(), true)) {
						$(item).hide()
					} else if (decide_Keyword($(item).find('.reply-con .text-con:first').text(), true)) {
						$(item).hide()
					} else {
						$(item).css('display', 'block')
					}
				})
			}
		} else if (OLD_URL.indexOf('search.bilibili.com') !== -1) {
			let video_item = $('.video-item');
			$.each(video_item, function(_item, item) {
				if (decide_Username($(item).find('a.up-name:first').text())) {
					$(item).hide()
				} else if (decide_Keyword($(item).find('a.title:first').text())) {
					$(item).hide()
				} else {
					$(item).css('display', '')
				}
			})
		}
	}
	function init_Bilibili() {
		let expand_div = document.createElement('div');
		expand_div.id = 'expandDiv';
		expand_div.title = 'bilibili_屏蔽设置';
		expand_div.setAttribute('style', 'display: block; position: fixed; bottom: 10px; right: 10px; float: right; border-radius: 19px; background-color: #00A1D6; color: #fff; font-size: 13px; cursor: pointer; z-index: 99999; height: 38px; width: 38px; line-height: 40px; text-align: center; border: 1px solid #00A1D6;');
		expand_div.innerText = '屏蔽';
		expand_div.addEventListener('click', function() {
			expand_Wrap('wrapDiv')
		}, false);
		let wrap_div = document.createElement('div');
		wrap_div.id = 'wrapDiv';
		wrap_div.setAttribute('style', 'position: fixed; bottom: 4vw; right: 1vw; z-index: 99999; background-color: #fff; text-align: left; display: block; padding: 0; margin: 0; border: 1px solid #a0a0a0; border-radius: 3px; color: #000; font-size: 13px; display: none;');
		let main_tag = document.createElement('fieldset');
		main_tag.id = 'mainTan';
		main_tag.setAttribute('style', 'border: 3px groove #00A1D6; border-radius: 3px; padding: 4px 9px 6px 9px; margin: 8px; min-width: 300px; width: auto; height: auto;');
		wrap_div.appendChild(main_tag);
		let legend_tag = document.createElement('legend');
		legend_tag.innerText = HEAD_TITLE;
		legend_tag.style.fontSize = '14px';
		main_tag.appendChild(legend_tag);
		let ul_tag = document.createElement('ul');
		ul_tag.setAttribute('style', 'list-style: none; padding-left: 0;');
		main_tag.appendChild(ul_tag);
		insert_Checkbox(ul_tag, CHECKBOX_VALUE.START_ARRAY, bilibili_config.functionEnable);
		insert_Checkbox(ul_tag, CHECKBOX_VALUE.USERNAME_ARRAY, bilibili_config.usernameEnable);
		insert_Checkbox(ul_tag, CHECKBOX_VALUE.KEYWORD_ARRAY, bilibili_config.keywordEnable);
		insert_Checkbox(ul_tag, CHECKBOX_VALUE.COMMENT_ARRAY, bilibili_config.commentEnable);
		let br = document.createElement('li');
		br.innerHTML = '------------------------------------------------------';
		ul_tag.appendChild(br);
		insert_Checkbox(ul_tag, CHECKBOX_VALUE.SAVE_CLOSE_ARRAY, bilibili_config.saveCloseEnable);
		let text_li = document.createElement('li');
		text_li.innerHTML = '<br />up主名字列表:(以 , 分隔)<br /><textarea id="userNameText" style="width: 96%; height: 80px; margin-left: 5px;">' + array_2_String(bilibili_config.usernameArray) + '</textarea>';
		ul_tag.appendChild(text_li);
		let keyword_li = document.createElement('li');
		keyword_li.innerHTML = '<br />关键字列表:(以 , 分隔)<br /><textarea id="keywordText" style="width: 96%; height: 80px; margin-left: 5px;">' + array_2_String(bilibili_config.keywordArray) + '</textarea>';
		ul_tag.appendChild(keyword_li);
		let save_button = document.createElement('button');
		save_button.id = 'saveButton';
		save_button.type = 'button';
		save_button.setAttribute('style', 'position: relative; float: right; margin-right: 5px; margin-top: 5px; cursor: pointer; background-color: #fb7299; border: 1px solid #fb7299; border-radius: 4px; padding: 2px 4px; color: #fff');
		save_button.innerText = '保存';
		save_button.title = '保存设置';
		ul_tag.appendChild(save_button);
		let cancel_button = document.createElement('button');
		cancel_button.id = 'cancelButton';
		cancel_button.type = 'button';
		cancel_button.setAttribute('style', 'position: relative; float: left; margin-left: 5px; margin-top:5px; cursor: pointer; background-color: #fb7299; border: 1px solid #fb7299; border-radius: 4px; padding: 2px 4px; color: #fff');
		cancel_button.innerText = '取消';
		cancel_button.title = '关闭设置';
		ul_tag.appendChild(cancel_button);
		save_button.addEventListener('click', function() {
			bilibili_config.functionEnable = document.getElementById(CHECKBOX_VALUE.START_ARRAY[0]).checked;
			bilibili_config.usernameEnable = document.getElementById(CHECKBOX_VALUE.USERNAME_ARRAY[0]).checked;
			bilibili_config.keywordEnable = document.getElementById(CHECKBOX_VALUE.KEYWORD_ARRAY[0]).checked;
			bilibili_config.commentEnable = document.getElementById(CHECKBOX_VALUE.COMMENT_ARRAY[0]).checked;
			bilibili_config.saveCloseEnable = document.getElementById(CHECKBOX_VALUE.SAVE_CLOSE_ARRAY[0]).checked;
			bilibili_config.usernameArray = string_2_Array(document.getElementById('userNameText').value);
			bilibili_config.keywordArray = string_2_Array(document.getElementById('keywordText').value);
			GM_setValue('bilibili_config', bilibili_config);
			hide_Event();
			if (bilibili_config.saveCloseEnable === true) {
				document.getElementById('wrapDiv').style.display = 'none'
			}
		}, false);
		cancel_button.addEventListener('click', function(e) {
			document.getElementById('wrapDiv').style.display = 'none';
			e.stopPropagation()
		}, false);
		console.log('bilibili_Block Ver.1.1.2: Console debug test. If there is no print prompt later, there is a problem with the script. Please feedback to me.');
		document.arrive('body', {
			fireOnAttributesModification: true,
			onceOnly: true,
			existing: true
		}, function() {
			console.log('bilibili_Block Ver.1.1.2: Insert block icon. The script works fine.');
			document.body.appendChild(expand_div);
			document.body.appendChild(wrap_div)
		})
	}
	Promise.all([GM_getValue('bilibili_config')]).then(function(data) {
		if (data[0] !== undefined) {
			bilibili_config = data[0]
		}
		init_Bilibili();
		hide_Event();
		try {
			GM_registerMenuCommand('bilibili_屏蔽设置', function() {
				document.getElementById('wrapDiv').style.display = 'block'
			})
		} catch (e) {
			console.log(e)
		}
		document.arrive('body', {
			fireOnAttributesModification: true,
			onceOnly: true,
			existing: true
		}, function() {
			try {
				let observer = new MutationObserver(function() {
					hide_Event()
				});
				let listener_container = document.querySelector("body");
				let option = {
					'childList': true,
					'subtree': true
				};
				observer.observe(listener_container, option)
			} catch (e) {
				console.log(e)
			}
		})
	}).
	catch (function(except) {
		console.log(except)
	})
})();