哔哩哔哩画中画弹幕

哔哩哔哩画中画支持显示弹幕

目前為 2022-08-20 提交的版本,檢視 最新版本

// ==UserScript==
// @name         哔哩哔哩画中画弹幕
// @namespace    qwq0
// @version      0.10
// @description  哔哩哔哩画中画支持显示弹幕
// @author       QwQ~
// @match        https://www.bilibili.com/video/*
// @match        https://www.bilibili.com/medialist/play/*
// @match        https://www.bilibili.com/bangumi/play/*
// @icon         data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==
// @grant        none
// ==/UserScript==

setTimeout(function() {
    'use strict';
    var videoHolder = null;
    var video = null;

    var width = 0;
    var height = 0;

    var canvas = document.createElement("canvas");
    var context = canvas.getContext("2d");
    var canvasWidth = canvas.width = 0;
    var canvasHeight = canvas.height = 0;
    var danmuFontsize = Math.floor(Math.min(canvasWidth, canvasHeight) / 14.5);
    var nVideo = document.createElement("video");

    var danmuList = [];
    var danmuLine = [];
    var danmuCount = 0;

    var danmuHolder = null;
    function addDanmu(text, color)
    {
        if(danmuList.length <= 25 || Math.random() < 25 / danmuList.length)
        {
            danmuCount++;
            var lineNum = 0;
            for(var i=0;i < 5 && danmuLine[lineNum] + 5 >= danmuCount;i++)
            {
                lineNum = Math.floor(Math.random() * 12);
            }
            danmuLine[lineNum] = danmuCount;
            danmuList.push({text: text, color: (color ? color : "rgb(255, 255, 255)"), x: canvasWidth, y: lineNum * danmuFontsize, w: context.measureText(text).width });
        }
    }
    var danmuObserver = new MutationObserver(e=>{
        e.forEach(o=>{
            // console.log("danmu(all)", o);
            if(o.type == "childList")
            {
                o.addedNodes.forEach(ele =>{
                    if(ele.innerText)
                    {
                        let text = ele.innerText;
                        addDanmu(text, ele.style.color);
                        console.log("danmu(it)", ele.style.color, text);
                    }
                    else if(ele.textContent)
                    {
                        let text = ele.textContent;
                        addDanmu(text, o.target.style.color);
                        console.log("danmu(ct)", o.target.color, text);
                    }
                });
            }
        });
    });
    setInterval(()=>{
        if(videoHolder == null)
        {
            videoHolder = document.getElementsByClassName("bilibili-player-video")[0] || document.getElementsByClassName("bpx-player-video-wrap")[0];
            video = videoHolder.children[0];
            video.addEventListener("enterpictureinpicture",() =>{
                nVideo.requestPictureInPicture();
                nVideo.play();
            });
        }
        var nowDanmuHolder = document.getElementsByClassName("bilibili-player-video-danmaku")[0] || document.getElementsByClassName("bpx-player-row-dm-wrap")[0];
        if(nowDanmuHolder != danmuHolder || width != video.videoWidth || height != video.videoHeight)
        {
            danmuHolder = nowDanmuHolder;
            danmuObserver.disconnect();
            width = video.videoWidth;
            height = video.videoHeight;
            canvasWidth = canvas.width = (height < 700 ? width : Math.floor(width / 2));
            canvasHeight = canvas.height = (height < 700 ? height : Math.floor(height / 2));
            danmuFontsize = Math.floor(Math.min(canvasWidth, canvasHeight) / 14.5);
            nVideo.srcObject = canvas.captureStream(60);
            setTimeout(() => nVideo.play(), 3500);
            danmuObserver.observe(danmuHolder, { childList: true, subtree: true });
            console.log("[哔哩哔哩画中画弹幕]", "视频切换")
            console.log("[哔哩哔哩画中画弹幕]", "视频分辨率", width, height);
            console.log("[哔哩哔哩画中画弹幕]", "渲染分辨率", canvasWidth, canvasHeight);
        }
    }, 1100);

    var lastTime = Date.now();
    function draw()
    {
        var nowTime = Date.now();
        var timeInterval = nowTime - lastTime;
        lastTime = nowTime;
        context.globalAlpha = 1;
        if(video)
        {
            context.drawImage(video, 0, 0, width, height, 0, 0, canvasWidth, canvasHeight);
        }
        context.font = danmuFontsize + 'px SimHei,"Microsoft JhengHei",Arial,Helvetica,sans-serif';
        context.textBaseline = "top";
        context.fillStyle = "rgb(255, 255, 255)";
        context.shadowColor = "rgb(0, 0, 0)";
        context.globalAlpha = 0.8;
        danmuList = danmuList.filter(o => (o.x >= -o.w));
        danmuList.forEach((o, i)=>{
            context.fillStyle = o.color;
            context.shadowBlur = 5;
            context.fillText(o.text, Math.floor(o.x), Math.floor(o.y));
            o.x -= timeInterval * 0.16;
        });
        requestAnimationFrame(draw);
    }
    requestAnimationFrame(draw);
    console.log("[哔哩哔哩画中画弹幕]", "已加载")
    console.log("[哔哩哔哩画中画弹幕]", "视频分辨率", width, height);
    console.log("[哔哩哔哩画中画弹幕]", "渲染分辨率", canvasWidth, canvasHeight);
}, 500);

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址