Greasy Fork

Greasy Fork is available in English.

Bilibili Live Danmaku Filter

使用一个简单的定时器把弹幕按照给定的正则表达式过滤一遍,征求更好的实现方式中

当前为 2019-06-24 提交的版本,查看 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Bilibili Live Danmaku Filter
// @namespace    http://tampermonkey.net/
// @version      0.2
// @description  使用一个简单的定时器把弹幕按照给定的正则表达式过滤一遍,征求更好的实现方式中
// @author       yuyuyzl
// @require      https://code.jquery.com/jquery-3.4.0.min.js
// @require      https://cdn.bootcss.com/jqueryui/1.12.1/jquery-ui.min.js
// @grant        unsafeWindow
// @grant        GM_getValue
// @grant        GM_setValue
// @grant        GM_setClipboard
// @grant        GM_info
// @match        *://live.bilibili.com/*
// ==/UserScript==

var BLDFRegex="【.*";
var BLDFReg;
var BLDFNeedSubBody;
var BLDFAutoStart;
var BLDFShowFilteredText;
var BLDFIntervalDelay;
var intervalID=-1;
(function() {
    'use strict';
    setTimeout(function(){
        if((BLDFRegex=GM_getValue("BLDFRegex"))==null)BLDFRegex="【.*】";
        if((BLDFNeedSubBody=GM_getValue("BLDFNeedSubBody"))==null)BLDFNeedSubBody=true;
        GM_setValue("BLDFNeedSubBody",BLDFNeedSubBody);
        if((BLDFShowFilteredText=GM_getValue("BLDFShowFilteredText"))==null)BLDFShowFilteredText=true;
        GM_setValue("BLDFShowFilteredText",BLDFShowFilteredText);
        if((BLDFAutoStart=GM_getValue("BLDFAutoStart"))==null)BLDFAutoStart=false;
        GM_setValue("BLDFAutoStart",BLDFAutoStart);
        if((BLDFIntervalDelay=GM_getValue("BLDFIntervalDelay"))==null)BLDFIntervalDelay=20;
        GM_setValue("BLDFIntervalDelay",BLDFIntervalDelay);
        $("head").append('<style type="text/css">\n' +
            '            .SubtitleBody{height:160px;background-color:rgba(0, 0, 0, 0.8);color:#fff;}\n' +
            '    .SubtitleBody.mobile{position:relative;top:5.626666666666667rem;}\n' +
            '    .SubtitleBody .title{padding:10px;font-size:14px;color:#ccc;}\n' +
            '    .SubtitleBody.mobile .title{font-size:12px;}\n' +
            '    .SubtitleBody .SubtitleTextBodyFrame{padding:0 10px;overflow:hidden;position:absolute;top:8px;bottom:8px;}\n' +
            '    .SubtitleBody .SubtitleTextBody{min-height:110px;font-size:14px;color:#ccc;}\n' +
            '    .SubtitleBody.mobile .SubtitleTextBody{font-size:12px;}\n' +
            '    .SubtitleBody .SubtitleTextBody p{margin-block-start:5px;margin-block-end:5px;}\n' +
            '    .SubtitleBody .SubtitleTextBody p:first-of-type{color:#fff;font-size:20px;font-weight:bold;}\n' +
            '    .SubtitleBody.mobile .SubtitleTextBody p:first-of-type{font-size:18px;}\n' +
            '    .SubtitleBody.Fullscreen{position:absolute;left:20px;bottom:30px;z-index:50;background-color:rgba(0, 0, 0, 0.6);width:800px;display:none}\n' +
            '    .SubtitleBody.mobile.Fullscreen{width:300px;}\n' +
            '    .bilibili-live-player[data-player-state=fullscreen] .SubtitleBody.Fullscreen,.bilibili-live-player[data-player-state=web-fullscreen] .SubtitleBody.Fullscreen{display:block;}\n' +
            '    .bilibili-lmp-video-wrapper[data-mode=fullScreen] .SubtitleBody.Fullscreen,.bilibili-lmp-video-wrapper[data-orientation=landscape] .SubtitleBody.Fullscreen{display:block;}\n' +
            '    .invisibleDanmaku{opacity:0 !important;}\n' +
            '    </style>');
        $(".icon-left-part").append('<span data-v-b74ea690="" id="regexSettings" title="正则屏蔽设置" class="icon-item icon-font icon-block"></span>');
        if(BLDFNeedSubBody){
            $("#gift-control-vm").before('<div class="SubtitleBody"><div style="height:100%;position:relative;"><div class="SubtitleTextBodyFrame"><div class="SubtitleTextBody"></div></div></div></div>');
            $(".bilibili-live-player").append('<div class="SubtitleBody Fullscreen ui-resizable"><div style="height:100%;position:relative;"><div class="SubtitleTextBodyFrame"><div class="SubtitleTextBody"></div></div></div><div class="ui-resizable-handle ui-resizable-e" style="z-index: 90;"></div><div class="ui-resizable-handle ui-resizable-s" style="z-index: 90;"></div><div class="ui-resizable-handle ui-resizable-se ui-icon ui-icon-gripsmall-diagonal-se" style="z-index: 90;"></div></div>');
            $(".SubtitleBody.Fullscreen").draggable();
        }
        var startInterval=function(){
            if(BLDFRegex==null)return;
            if(intervalID>=0)clearInterval(intervalID);
            BLDFReg=new RegExp(BLDFRegex);
            intervalID=setInterval(function (){$(".bilibili-danmaku").each(function(i,obj){
                    if(!(obj.innerText[obj.innerText.length-1]==" ")){

                        var matchres=obj.innerText.match(BLDFReg);
                        if(matchres!=null){
                            $(obj).removeClass("invisibleDanmaku");
                            //console.log(matchres);
                            $('.SubtitleTextBody').prepend("<p>"+matchres+"</p>");
                            $('.SubtitleTextBody').each(function(i,obj){
                                $(obj).children().each(function(i,obj){
                                if(i>=6){
                                    obj.remove();
                                }
                                });
                                });
                        }else {$(obj).addClass("invisibleDanmaku");}
                        if(BLDFShowFilteredText)obj.innerText=matchres+' ';else obj.innerText=obj.innerText+' ';
                    }
                })},BLDFIntervalDelay);
        }
        if(BLDFAutoStart)startInterval();
        $("#regexSettings").click(function(){
            BLDFRegex=prompt("输入白名单正则表达式,留空则关闭过滤",BLDFRegex);
            if(BLDFRegex==null)return;
            if(intervalID>=0)clearInterval(intervalID);
            BLDFReg=new RegExp(BLDFRegex);
            if(BLDFRegex!=""){
                startInterval();
                GM_setValue("BLDFRegex",BLDFRegex);
            }
        });
    },3000);


})();