BiliOnlineHook

B站直播间同接数显示

当前为 2023-11-26 提交的版本,查看 最新版本

// ==UserScript==
// @name         BiliOnlineHook
// @description  B站直播间同接数显示
// @namespace    http://tampermonkey.net/
// @version      0.0.3
// @author       jeffz615
// @match        *://live.bilibili.com/*
// @icon         https://live.bilibili.com/favicon.ico
// @sandbox      raw
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';
    function hook_wrapper() {
        let g_rank_count = 0;
        let g_online_count = 0;

        function on_online_rank_count(obj) {
            const rank_count = obj.data.count;
            const online_count = obj.data.online_count;
            let flag = false;
            if (rank_count && rank_count !== g_rank_count) {
                g_rank_count = rank_count;
                flag = true;
            }
            if (online_count && online_count !== g_online_count) {
                g_online_count = online_count;
                flag = true;
            }
            if (flag) {
                const showers = document.querySelectorAll("#rank-list-ctnr-box > div.tabs > ul > li.item.live-skin-normal-text.dp-i-block.live-skin-separate-border.border-box.t-center.pointer")
                if (showers.length > 0) {
                    showers[0].innerText = '高能用户(' + g_rank_count + '/' + g_online_count + ')';
                    showers[0].setAttribute('title', '除号左边是贡献值非0人数,右边是所有人数。' + (g_online_count === 0 ? '' : ('计算结果:' + (g_rank_count / g_online_count * 100).toFixed(2) + '%')));
                }
            }
        }

        const cb_map = {
            "ONLINE_RANK_COUNT": on_online_rank_count,
        };

        Array.prototype.push = new Proxy(Array.prototype.push, {
            apply(target, thisArg, argArray) {
                try {
                    if (argArray && argArray.length > 0) {
                        for (let i = 0; i < argArray.length; i++) {
                            if (argArray[i] && argArray[i].cmd) {
                                if (cb_map[argArray[i].cmd]) {
                                    cb_map[argArray[i].cmd](argArray[i]);
                                }
                            } else {
                                break;
                            }
                        }
                    }
                } catch (e) {
                    console.error(e);
                }
                return Reflect.apply(target, thisArg, argArray);
            }
        });
    }
    hook_wrapper();
})();