弹幕显示名称

直播弹幕显示名称,右键弹幕高亮uid

目前为 2021-04-05 提交的版本。查看 最新版本

// ==UserScript==
// @name         弹幕显示名称
// @namespace    http://tampermonkey.net/
// @version      0.6
// @description  直播弹幕显示名称,右键弹幕高亮uid
// @author       You
// @match        https://live.bilibili.com/*
// @grant GM_setValue
// @grant GM_getValue
// @run-at document-end
// ==/UserScript==

(function() {
    'use strict';
    document.body.insertAdjacentHTML('afterBegin',`<style>
nav,.wrap{   width: auto !important;   max-width: 1400px !important;   margin: auto;}
.wrap>ul{    display: flex !important;    flex-wrap: wrap;    justify-content: center;}
.bilibili-danmaku i{font-size:60%;text-decoration: underline;opacity:1}
.bilibili-danmaku .chat-history-panel {display:inline-block;background:none;}
.bilibili-danmaku .chat-history-panel *{margin: 0 1px 0 2px !important; padding:0 !important; vertical-align: middle;}
.highline{ outline: 3px solid; }
</stype>`);
    //本地储存
    let localdata = new Proxy({},{
        get(self,key){
            let list = GM_getValue('userlist',self);
            return list[key];
        },
        set(self,key,val){
            self = GM_getValue('userlist',self)
            if(val===null) delete self[key];
            else self[key] = val;
            GM_setValue('userlist', self);
            return true;
        }
    });

    //高亮边框&右键
    let menuObserver = new MutationObserver((mu,obs)=>{
        mu.forEach(xx=>{
            let el = xx.addedNodes[0];
            if(!el||el.className.trim()!="context-menu-submenu"){
                return;
            }
            let ul = el.querySelector('.context-menu-ul');
            let uid = el.querySelector('[href*=http]').getAttribute('href').match(/\d+$/)[0];
            let name = document.querySelector(`.chat-history-list [data-uid="${uid}"]`).dataset.uname;
            el.setAttribute('title',name);
            el.querySelectorAll('[title]').forEach(xx=>xx.removeAttribute('title'));
            ul.insertAdjacentHTML('beforeEnd',`<li class="context-menu-function"><a data-uid='${uid}' data-name='${name}' class="xx">高亮uid/取消高亮</a></li>`);
        });
        document.querySelectorAll('.bilibili-live-player-context-menu-container .xx').forEach(xx=>{
            xx.onclick=xxx=>{
                xxx = xxx.target;
                localdata[xxx.dataset.uid] = localdata[xxx.dataset.uid]?null:xxx.dataset.name;
                document.querySelectorAll(`.bilibili-live-player-video-danmaku [data-uid="${xxx.dataset.uid}"]`).forEach(xx=>{
                    localdata[xxx.dataset.uid] ? xx.classList.add('highline') : xx.classList.remove('highline') ;
                });
                document.querySelectorAll('.bilibili-live-player-context-menu-container').forEach(xx=>{
                    xx.setAttribute('style','top: -9999px; left: -9999px;');
                })
            };
        });
    });
    //名称备注
    let danmakuObserver = new MutationObserver((mu, obs) => {
        document.querySelectorAll('.bilibili-live-player-video-danmaku>:not([style*=done])').forEach(xx=>{
            if(xx.querySelector('i')){
                xx.setAttribute('style',xx.getAttribute('style')+(';done'));
                return;
            }
            let tt = xx.innerHTML.replaceAll('\\','\\\\').replaceAll('"','\\"');
            let lw = Array.from(document.querySelectorAll(`.chat-items>.danmaku-item[data-danmaku*="${tt}"]:not([style*=done])`)||[]).pop();
            let st = xx.getAttribute('style')+';done';
            let ts = parseFloat( st.match(/-webkit-transform/) ? (st.match(/transform\s+([\d\.]+)/)||[0,0])[1] : -1 );
            if(lw && ts){
                xx.setAttribute('data-uid',lw.dataset.uid);
                lw.setAttribute('style',lw.getAttribute('style')+(';done'));
                let ic = lw.querySelector('[class*=-icon]:empty');//加图标
                ic && (xx.innerHTML+= `<b class='chat-history-panel'><b class='chat-history-list'><b class='chat-item danmaku-item'>${ic.outerHTML}</b></b></b>`);
                xx.innerHTML+=`<i style=';'>${lw.dataset.uname}</i>`;
                localdata[xx.dataset.uid] && xx.classList.add('highline') ;


                xx.setAttribute('style', st);
                (ts>0) && setTimeout(xxx=>{
                    xx.setAttribute('style',`done;display:none`);
                    xx.classList.remove('highline');
                },ts*1000);
                if(ts<5){console.log([xx.outerHTML,ts,xx]);}

            }
        });
    });
    //活动页要等菜单加载?
    setTimeout(xx=>{
        document.querySelectorAll('.bilibili-live-player-context-menu-container>ul').forEach(dm=>{
            menuObserver.observe(dm, { attributes: false, childList: true, subtree: false } );
        })
        document.querySelectorAll('.chat-items,.bilibili-live-player-video-danmaku').forEach(dm=>{
            danmakuObserver.observe(dm, { attributes: false, childList: true, subtree: true } );
        });
    },500);
})();

QingJ © 2025

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