Greasy Fork 还支持 简体中文。

Bilibili番剧显示单集信息

看番时让B站显示单集的的播放量和弹幕量

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Bilibili番剧显示单集信息
// @namespace    http://tampermonkey.net/
// @version      3.0
// @include     http*://www.bilibili.com/bangumi/play/ss*
// @include     http*://www.bilibili.com/bangumi/play/ep*
// @description  看番时让B站显示单集的的播放量和弹幕量
// @author       ementt
// @grant        none
// @license MIT
// ==/UserScript==
(function() {
    'use strict';

    const url = 'https://api.bilibili.com/x/web-interface/view';
    let all_view;


    async function getBvid(ep_id) {
        try {
            const res = await fetch(`https://api.bilibili.com/pgc/view/web/season?ep_id=${ep_id}`);
            const data = await res.json();
            const ep = data.result.episodes.find(e => e.ep_id == ep_id);
            console.log("当前 bvid:", ep.bvid);
            return ep.bvid; // 可以返回给调用方
        } catch (err) {
            console.error(err);
        }
    }

    function search() {
        const mediaCount = document.querySelector('.mediainfo_mediaDesc__jjRiB');
        if (!mediaCount) {
            console.log("media count not found");
            return};

        const _view = mediaCount.textContent.split('·')[0];
        if (!all_view) {
            all_view = _view.substring(0, _view.length - 4) + '总播放';
        }
        (async () => {
            const ep_id = location.pathname.match(/ep(\d+)/)[1];
            const bvid = await getBvid(ep_id);
            console.log("拿到 bvid:", bvid);

            fetch(`${url}?bvid=${bvid}`)
                .then(res => res.json())
                .then(response => {
                const data = response.data;
                console.log(data);
                const view = Number(data.stat.view);
                const danmaku = Number(data.stat.danmaku);
                const coin = Number(data.stat.coin);

                let view_write = '--';
                if (view) {
                    view_write = view < 10000 ? view : Math.floor(view / 10000) + '万';
                }

                let danmu_write = '--';
                if (danmaku) {
                    danmu_write = danmaku < 10000 ? danmaku : Math.floor(danmaku / 10000) + '万';
                }

                const coinSpan = document.querySelector('#toolbar_module > div.coin-info > span');
                if (coinSpan) {
                    coinSpan.textContent = coin;
                }

                const text2 = mediaCount.textContent.split('·')[2] || '';
                mediaCount.textContent = `${view_write}播放  ·  ${danmu_write}弹幕  ·${text2}  ·  ${all_view}`;
            })
                .catch(err => {
                console.error('请求失败:', err);
            });
        })();

    }
    function bind() {

        const aidElement = document.querySelector("#__next > div.home-container > div.main-container > div.plp-l.sticky > div > div.mediainfo_mediaInfoWrap__nCwhA > div > div:nth-child(3) > span:nth-child(4) > span > a");
        if (!aidElement) return;

        // 监听 DOM 变化
        const observer = new MutationObserver(() => {
            console.log("find new episode")
            search();
        });
        observer.observe(aidElement, { childList: true,
                                      attributes: true,
                                      characterData: true,
                                      subtree: true});
    }

    bind();

    setTimeout(search, 2000)
    setTimeout(bind, 2000)

})();