前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >b站评论过滤脚本

b站评论过滤脚本

作者头像
时光潜流
发布2022-12-26 19:48:19
7190
发布2022-12-26 19:48:19
举报
文章被收录于专栏:博客专栏

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

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

  油猴脚本源代码:

代码语言:javascript
复制
// ==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)
    }
})();
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022-06-08,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档