// ==UserScript== // @name 哔哩哔哩 - 屏蔽指定内容 // @namespace https://greasyfork.org/zh-CN/users/193133-pana // @homepage https://www.sailboatweb.com // @version 2.1.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_setClipboard // @grant GM_registerMenuCommand // @grant GM_info // @run-at document-start // @note ver.2.1.0 添加允许按正则表达式进行屏蔽的功能 // @note ver.2.0.0 调整了添加与删除关键字的方式,方便操作; 将评论与视频标题的关键词分开作用 // @note ver.1.2.1 完善部分未被覆盖的页面内容 // @note ver.1.2.0 添加屏蔽评论的功能 // @note ver.1.1.2 调整屏蔽按钮的位置到右下角; 尝试处理脚本偶尔会失效的问题 // @note ver.1.1.1 修复搜索页面以关键字屏蔽无效的问题 // @note ver.1.1.0 匹配视频播放页面; 优化代码 // @downloadURL none // ==/UserScript== (function() { 'use strict'; const HEAD_TITLE = 'bilibili_屏蔽设置 ver.' + GM_info.script.version; const OLD_URL = location.href; const CHECKBOX_VALUE = { START_ARRAY: ['startCheckbox', '启用屏蔽功能'], USERNAME_ARRAY: ['usernameCheckbox', '按up主'], KEYWORD_ARRAY: ['keywordCheckbox', '按关键字'], REG_ARRAY: ['regCheckbox', '按正则表达式'], COMMENT_USERNAME_ARRAY: ['commentUsernameCheckbox', '按up主'], COMMENT_KEYWORD_ARRAY: ['commentKeywordCheckbox', '按关键字'], COMMENT_REG_ARRAY: ['commentRegCheckbox', '按正则表达式'], SAVE_CLOSE_ARRAY: ['saveCloseCheckbox', '保存的同时关闭设置框'], }; const INPUT_VALUE = { ADD_USERNAME_ARRAY: ['addUsernameInput', '添加up主:', '(添加多个时以,分隔)'], ADD_VIDEO_TITLE_ARRAY: ['addVideoTitleInput', '添加视频标题关键字:', '(添加多个时以,分隔)'], ADD_COMMENT_ARRAY: ['addCommentInput', '添加评论关键字:', '(添加多个时以,分隔)'], ADD_VIDEO_TITLE_REG_ARRAY: ['addVideoTitleRegInput', '添加视频标题正则表达式:', '( / 建议写成 \\/ )'], ADD_VIDEO_TITLE_MODIFIER_ARRAY: ['addVideoTitleModifierInput', '修饰符:', '(如:gim)'], ADD_COMMENT_REG_ARRAY: ['addCommentRegInput', '添加评论正则表达式:', '( / 建议写成 \\/ )'], ADD_COMMENT_MODIFIER_ARRAY: ['addCommentModifierInput', '修饰符:', '(如:gim)'] }; const TEXTAREA_VALUE = { USERNAME_ARRAY: ['usernameTextarea', 'up主列表:'], VIDEO_TITLE_KEYWORD_ARRAY: ['videTitleKeywordTextarea', '视频标题关键字列表:'], COMMENT_KEYWORD_ARRAY: ['commentKeywordTextarea', '评论关键字列表:'], VIDEO_TITLE_REG_ARRAY: ['videoTitleRegTextarea', '视频标题正则表达式列表:'], COMMENT_REG_ARRAY: ['commentRegTextarea', '评论正则表达式列表:'] }; var bilibili_config = { functionEnable: true, usernameEnable: true, keywordEnable: true, commentEnable: false, commentKeywordEnable: false, saveCloseEnable: true, regEnable: false, commentRegEnable: false, usernameArray: [], keywordArray: [], commentArray: [], regArray: [], commentRegArray: [] }; 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 extract_Array(list_id, reg_status = false) { let re_arr = []; let list_dom = document.getElementById(list_id); let list_arr = list_dom.getElementsByClassName('BilibiliTextSpan'); for (let i = list_arr.length - 1; i >= 0; i--) { let temp_value; if (list_arr[i].title) { temp_value = list_arr[i].title } else { temp_value = list_arr[i].innerText } if (reg_status) { re_arr.push(new RegExp(temp_value.replace(/^\/|\/[a-z]*$/gi, ''), temp_value.replace(/^\/.*\/[^a-z]*$/i, ''))) } else { re_arr.push(temp_value) } } return re_arr } function add_Child(input_tag, list_id) { let text_value = input_tag.value; if (text_value) { let text_arr = string_2_Array(text_value); let save_arr = extract_Array(list_id); text_arr.forEach(function(item) { if (!save_arr.includes(item)) { $('#' + list_id).prepend(create_Child(item)) } }) } input_tag.value = '' } function add_Reg_Child(reg_input, modifier_input, list_id) { let reg_value = reg_input.value; let modifier_value = modifier_input.value; if (reg_value) { try { let reg_str = new RegExp(reg_value, modifier_value); let save_arr = extract_Array(list_id); if (!save_arr.includes(reg_str.toString())) { $('#' + list_id).prepend(create_Child(reg_str.toString())) } reg_input.value = ''; modifier_input.value = '' } catch (e) { console.log('bilibili_Block Ver.2.1.0: Invalid regular expression.'); console.log(e) } } } 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_Separator(ul_node, text_value = '') { let temp_li = document.createElement('li'); if (text_value) { temp_li.innerHTML = '' + text_value + '' } else { temp_li.innerHTML = '
' } ul_node.appendChild(temp_li) } function insert_Checkbox(ul_node, li_array, check_enable_value, margin_status = true) { let temp_li = document.createElement('li'); temp_li.style.display = 'inline-block'; if (margin_status) { temp_li.style.marginLeft = '15px' } temp_li.innerHTML = ''; ul_node.appendChild(temp_li) } function insert_Input(ul_node, li_array, list_id) { let temp_li = document.createElement('li'); let temp_div = document.createElement('div'); temp_div.innerText = li_array[1]; temp_div.style.marginTop = '10px'; let temp_input = document.createElement('input'); temp_input.id = li_array[0]; temp_input.placeholder = li_array[2]; temp_input.setAttribute('style', 'margin-left: 5px; margin-right: 5px; width: 150px'); $(temp_input).keyup(function(e) { if (e.keyCode === 13) { add_Child(temp_input, list_id) } }); let temp_span = document.createElement('span'); temp_span.innerText = '+'; temp_span.title = '添加'; temp_span.setAttribute('style', 'display: inline-block; width: 18px; height: 18px; background-color: #fb7299; color: #fff; cursor: pointer; border: 1px solid #fb7299; border-radius: 9px; text-align: center; box-shadow: 0 0 4px blue;'); temp_span.addEventListener('click', function() { add_Child(temp_input, list_id) }); temp_div.appendChild(temp_input); temp_div.appendChild(temp_span); temp_li.appendChild(temp_div); ul_node.appendChild(temp_li) } function insert_Reg_Input(ul_node, reg_array, modifier_array, list_id) { let temp_li = document.createElement('li'); let temp_div = document.createElement('div'); temp_div.style.marginTop = '20px'; let reg_span = document.createElement('span'); reg_span.innerText = reg_array[1]; let reg_input = document.createElement('input'); reg_input.id = reg_array[0]; reg_input.placeholder = reg_array[2]; reg_input.setAttribute('style', 'margin-left: 5px; margin-right: 5px; width: 100px'); let modifier_span = document.createElement('span'); modifier_span.innerText = modifier_array[1]; let modifier_input = document.createElement('input'); modifier_input.id = modifier_array[0]; modifier_input.placeholder = modifier_array[2]; modifier_input.setAttribute('style', 'margin-left: 5px; margin-right: 5px; width: 50px'); let btn_span = document.createElement('span'); btn_span.innerText = '+'; btn_span.title = '添加正则表达式'; btn_span.setAttribute('style', 'display: inline-block; width: 18px; height: 18px; background-color: #fb7299; color: #fff; cursor: pointer; border: 1px solid #fb7299; border-radius: 9px; text-align: center; box-shadow: 0 0 4px green;'); btn_span.addEventListener('click', function() { add_Reg_Child(reg_input, modifier_input, list_id) }); modifier_span.appendChild(modifier_input); reg_span.appendChild(reg_input); temp_div.appendChild(reg_span); temp_div.appendChild(modifier_span); temp_div.appendChild(btn_span); temp_li.appendChild(temp_div); ul_node.appendChild(temp_li) } function insert_Textarea(ul_node, li_array, save_array, reg_status = false) { let temp_li = document.createElement('li'); let temp_div = document.createElement('div'); temp_div.innerText = li_array[1]; temp_div.style.marginTop = '5px'; let list_div = document.createElement('div'); list_div.className = 'BilibiliListDiv'; list_div.id = li_array[0]; list_div.setAttribute('style', 'border: 1px dotted #00a1d6; max-height: 60px; overflow: auto; margin-top: 3px; min-height: 3px;'); $.each(save_array, function(_index, item) { if (item) { if (reg_status) { item = item.toString() } $(list_div).prepend(create_Child(item)) } }); let clear_btn = document.createElement('span'); clear_btn.innerText = '清空'; clear_btn.title = '一键清空'; clear_btn.setAttribute('style', 'cursor: pointer; margin-left: 10px; color: #fff; background-color: gray; border: 1px solid gray; border-radius: 5px;'); clear_btn.addEventListener('click', function() { $(list_div).empty() }); let copy_btn = document.createElement('span'); copy_btn.innerText = '复制'; copy_btn.title = '复制列表'; copy_btn.setAttribute('style', 'cursor: pointer; margin-left: 10px; color: #fff; background-color: gray; border: 1px solid gray; border-radius: 5px;'); copy_btn.addEventListener('click', function() { GM_setClipboard(array_2_String(extract_Array(li_array[0]))); this.style.color = 'cyan'; this.title = '复制完成' }); temp_div.appendChild(clear_btn); temp_div.appendChild(copy_btn); temp_div.appendChild(list_div); temp_li.appendChild(temp_div); ul_node.appendChild(temp_li) } function create_Child(text_value) { let temp_span = document.createElement('span'); temp_span.setAttribute('style', 'margin: 3px; background-color: #fdde80; border: 1px solid #fdde80; border-radius: 5px; padding: 2px; display: inline-block;'); let text_span = document.createElement('span'); text_span.className = 'BilibiliTextSpan'; text_span.innerText = (text_value.length > 9 ? text_value.slice(0, 3) + '...' + text_value.slice(-3) : text_value); if (text_value.length > 9) { text_span.title = text_value } text_span.setAttribute('style', 'border-right: 1px solid red; margin-right: 4px; padding-right: 4px;'); let del_span = document.createElement('span'); del_span.innerText = 'X'; del_span.title = '移除'; del_span.setAttribute('style', 'cursor: pointer; color: red;'); del_span.addEventListener('click', function() { $(temp_span).remove() }); temp_span.appendChild(text_span); temp_span.appendChild(del_span); return temp_span } function decide_Keyword(str, is_comment = false) { if (bilibili_config.functionEnable && str) { if (is_comment) { if (bilibili_config.commentKeywordEnable) { for (let i = 0; i < bilibili_config.commentArray.length; i++) { if (bilibili_config.commentArray[i] && str.indexOf(bilibili_config.commentArray[i]) !== -1) { return true } } } if (bilibili_config.commentRegEnable) { try { for (let j = 0; j < bilibili_config.commentRegArray.length; j++) { if (bilibili_config.commentRegArray[j].test(str)) { return true } } } catch (e) { console.log('bilibili_BLock Ver.2.1.0: Invalid regular expression for comparison.'); console.log(e); return false } } } else { if (bilibili_config.keywordEnable) { for (let k = 0; k < bilibili_config.keywordArray.length; k++) { if (bilibili_config.keywordArray[k] && str.indexOf(bilibili_config.keywordArray[k]) !== -1) { return true } } } if (bilibili_config.regEnable) { try { for (let l = 0; l < bilibili_config.regArray.length; l++) { if (bilibili_config.regArray[l].test(str)) { return true } } } catch (e) { console.log('bilibili_BLock Ver.2.1.0: Invalid regular expression for comparison.'); console.log(e); return false } } } } return false } function decide_Username(username, is_comment = false) { if (bilibili_config.functionEnable && username) { if (is_comment) { if (bilibili_config.commentEnable) { return bilibili_config.usernameArray.includes(username) } } else { if (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()) || decide_Keyword($(item).find('p.title:first').text()) || decide_Keyword($(item).find('div.txt a.link p:first').text())) { $(item).hide() } else { $(item).css('display', '') } }); let article_card = $('.article-card'); $.each(article_card, function(_index, item) { if (decide_Username($(item).find('a.up:first').text())) { $(item).hide() } else if (decide_Keyword($(item).find('a.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', '') } }); let read_rank_list = $('.rank-list li.item'); $.each(read_rank_list, function(_index, item) { if (decide_Keyword($(item).find('> a:first').text())) { $(item).hide() } else { $(item).css('display', 'block') } }); 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') } }) } if (OLD_URL.indexOf('www.bilibili.com/read/ranking')) { let article_list = $('.article-list li'); $.each(article_list, function(_index, item) { if (decide_Username($(item).find('.nick-name:first').text())) { $(item).hide() } else if (decide_Keyword($(item).find('.article-title:first').text())) { $(item).hide() } else { $(item).css('display', '') } }) } } 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: 1vh; right: 1vw; float: right; border-radius: 19px; background-color: #00A1D6; color: #fff; font-size: 13px; cursor: pointer; z-index: 99999; height: 38px; width: 38px; line-height: 38px; 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: 6vh; 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; width: 450px;'); 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.width = 'auto'; legend_tag.innerHTML = '