b站分P视频时间计算器

...

目前為 2024-11-12 提交的版本,檢視 最新版本

// ==UserScript==
// @name         b站分P视频时间计算器
// @namespace    http://tampermonkey.net/
// @version      2024-11-12
// @author       You
// @description  ...
// @match        https://www.bilibili.com/video/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=bilibili.com
// @grant        none
// @license      MIT
// ==/UserScript==
// noinspection DuplicatedCode

(function () {
    'use strict';

    let currentBV = null; // Variable to store the current BV number
    let videoData = null; // Variable to store the fetched data

    async function fetchData() {
        try {
            const bv = window.location.pathname.match(/\/video\/(BV\w+)/)[1];

            // If the BV number hasn't changed, return the existing data
            if (bv === currentBV && videoData !== null) {
                return videoData;
            }

            currentBV = bv; // Update the current BV number
            const apiUrl = `https://api.bilibili.com/x/player/pagelist?bvid=${bv}`;
            const response = await fetch(apiUrl);
            const result = await response.json();
            videoData = result.data; // Store the fetched data
            console.log('bv', bv);
            return videoData;
        } catch (error) {
            console.error('Error fetching data:', error);
            return [];
        }
    }

    window.addEventListener("load", () => {
        const btn = document.createElement('button');
        btn.innerText = '计算时间';
        btn.style.cssText = 'position: fixed; right: 10px; top: 300px; width: 70px; padding: 5px;';
        document.body.appendChild(btn);
        btn.onclick = () => calculateTime(null);
        try {
            //监听地址栏
            history.pushState = async () => {
                const data = await fetchData();
                if (data.length <= 1) {
                    btn.innerText = "不是分P";
                    setTimeout(() => btn.hidden = true, 1200);
                } else {
                    btn.hidden = false;
                    await calculateTime(data);
                }
            }
        } catch (err) {
            btn.innerText = err.message;
        }

        async function calculateTime(data) {
            if (!data) {
                data = await fetchData();
            }
            let totalSeconds = 0;
            const urlParams = new URLSearchParams(window.location.search);
            let p = urlParams.get('p') ? parseInt(urlParams.get('p'), 10) : 1;
            console.log("p为", p, "总集数为", data.length);
            for (let i = p - 1; i < data.length; i++) {
                totalSeconds += data[i].duration;
            }
            let hours = Math.floor(totalSeconds / 3600);
            let minutes = Math.floor((totalSeconds % 3600) / 60);
            let seconds = totalSeconds % 60;
            let formattedTime = `${hours.toString().padStart(2, '0')}:${minutes.toString().padStart(2, '0')}:${seconds.toString().padStart(2, '0')}`;
            if (hours < 2) {
                btn.innerText = `${formattedTime}\n还有不到2小时,稳啦`;
            } else {
                btn.innerText = formattedTime;
            }
        }
    });
})();

QingJ © 2025

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