b站评论过滤脚本

前往原站点查看

2022-06-08 01:14:11

  今天乘有空,写了一个油猴脚本,可以屏蔽b站评论区的评论!

  油猴脚本地址:点击前往 ,相关的修改方式都有所描述。

  油猴脚本源代码:

// ==UserScript==
// @name bili_rebuild
// @description b站评论过滤器
// @license MIT
// @namespace dreamcenter
// @version 0.0.0.1
// @match *://www.bilibili.com/*
// @require http://code.jquery.com/jquery-3.5.1.js
// @grant GM_xmlhttpRequest
// ==/UserScript==

let banMap = [
    "test","随机",'恶心','病','纯','ch','CH','op','策划'
];

// 是否对屏蔽条目隐藏[true/false]
let hiddenMode = true

// 是否连接到云屏蔽词库[true/false]
let cloudBanMap = false

// 是否控制台打印屏蔽日志[true/false]
let logShield = false


/*******************************下方内容不要修改***************************************/

function filterStr(item){
    var str=''
    var childs = item.childNodes;
    childs.forEach((subitem,index)=>{
        if(subitem.nodeType===3){
            str = subitem.nodeValue;
            for(var i=0;i<banMap.length;i++){
                str = str.replaceAll(banMap[i],'█')
            }
            subitem.nodeValue = str;
        }
    })
}

function hiddenWay(item){
    var str=''
    var childs = item.childNodes;
    childs.forEach((subitem,index)=>{
        if(subitem.nodeType===3){
            str = subitem.nodeValue;
            for(var i=0;i<banMap.length;i++){
                str = str.replaceAll(banMap[i],'█(' + banMap[i] + ')')
            }
            subitem.nodeValue = str;

            if(str.indexOf('█')!=-1){

                if (logShield) console.log('!  :\n' + subitem.nodeValue)

                if (subitem.parentNode.nodeName.toLocaleLowerCase() == 'span'){
                    // subitem.parentNode.parentNode.parentNode.parentNode.style.color='red'
                    subitem.parentNode.parentNode.parentNode.parentNode.style.display='none'
                }
                else{
                    // subitem.parentNode.parentNode.style.border='1px solid red'
                    subitem.parentNode.parentNode.style.display='none'
                }
            }
        }
    })
}

// https://github.com/dreamcenter/dreamcenter.github.io/blob/master/test
function cloud(){
    GM_xmlhttpRequest({
        method: 'GET',
        //url: "http://localhost:8080/filter/api/shield/test",
        url: "https://dreamcenter.github.io/test",
        onload: function (res) {
            /*var list = JSON.parse(res.response)
            list.forEach((item,index)=>{
                banMap.push(item.word)
            })
            console.log(banMap)*/
            let str = res.response.replace('\n','')
            banMap.push(...str.split(','))
            console.log(banMap)

        },
        onerror: function (err) {
            alert("服务器连接失败")
        }
    })
}

(function () {
    'use strict';
    window.onload = function(){

        // connect to the cloud ban map to get common ban map
        if(cloudBanMap) cloud()

        // detect and filter
        setInterval(()=>{
            let replies = document.querySelectorAll('p.text:not(p[filtered]),span.text-con:not(span[filtered])'); //document.getElementsByClassName('p.text');
            replies.forEach((item,index)=>{
                if (hiddenMode)
                    hiddenWay(item)
                else{
                    filterStr(item)
                }
                item.setAttribute('filtered',true)
            })
        },100)
    }
})();


上一篇: sql导入本地与MimeMessage
下一篇: 音乐播放组件