为 b 站 (bilibili) 添加自定义倍速, 支持快捷键,下一个视频的倍速=上一个视频的倍速

为b站(bilibili)添加更多倍速 (可自定义, 支持自定义快捷键): 0.1X; 3X; 4X; 5X。

目前為 2024-10-18 提交的版本,檢視 最新版本

// ==UserScript==
// @name           为 b 站 (bilibili) 添加自定义倍速, 支持快捷键,下一个视频的倍速=上一个视频的倍速
// @namespace      /DBI/bili-more-rates
// @version        1.2.3
// @description    为b站(bilibili)添加更多倍速 (可自定义, 支持自定义快捷键): 0.1X; 3X; 4X; 5X。
// @description    解题思路:在这个脚本中,我们首先等待播放器加载,然后清空现有的倍速菜单项,并按照指定的顺序(0.1, 0.5, 0.75, 1, 1.25, 1.5, 2, 3, 4, 5)添加倍速到播放器菜单中。
// @description              如果存在存储的倍速值,则将播放器的倍速设置为该值。最后,我们监听快捷键事件,以便用户可以通过快捷键切换倍速。
// @author         DuckBurnIncense
// @match          https://www.bilibili.com/video/*
// @match          https://www.bilibili.com/list/watchlater*
// @match          https://www.bilibili.com/bangumi/play/*
// @icon           https://www.bilibili.com/favicon.ico
// @supportURL     https://gf.qytechs.cn/zh-CN/scripts/462473/
// @grant          GM_addStyle
// @grant          GM_registerMenuCommand
// @grant          GM_setValue
// @grant          GM_getValue
// @grant          unsafeWindow
// @run-at         document-end
// @license        MIT
// ==/UserScript==

(function () {
    'use strict';

    // 等待播放器加载
    function waitForPlayer(callback) {
        const checkPlayer = () => {
            if (document.querySelector('.bpx-player-ctrl-playbackrate-menu')) {
                callback();
            } else {
                setTimeout(checkPlayer, 500);
            }
        };
        checkPlayer();
    }

    waitForPlayer(() => {
        const currentRate = GM_getValue('currentRate'); // 获取存储的当前倍速
        const domVideoElement = document.querySelector('video.bpx-player-video'); // 获取视频元素

        // 自定义倍速和快捷键
        const customRates = [
            { rate: 0.1, shortcut: '' },
            { rate: 3, shortcut: 'shift+3' },
            { rate: 4, shortcut: 'shift+4' },
            { rate: 5, shortcut: 'shift+5' },
        ];

        // 标准倍速
        const standardRates = [0.5, 0.75, 1, 1.25, 1.5, 2];

        // 合并并去重倍速
        const allRates = [...new Set([...standardRates, ...customRates.map(r => r.rate)])];

        // 按照指定顺序排序倍速
        const orderedRates = [0.1, 0.5, 0.75, 1, 1.25, 1.5, 2, 3, 4, 5].filter(rate => allRates.includes(rate));

        // 将倍速添加到播放器菜单
        const menu = document.querySelector('.bpx-player-ctrl-playbackrate-menu');
        menu.innerHTML = ''; // 清空现有菜单项

        orderedRates.forEach((rate) => {
            const newRateNode = document.createElement('li');
            newRateNode.innerText = `${rate}x`;
            newRateNode.classList.add('bpx-player-ctrl-playbackrate-menu-item');
            newRateNode.dataset.value = rate;

            // 绑定点击事件
            newRateNode.addEventListener('click', () => {
                domVideoElement.playbackRate = rate;
                GM_setValue('currentRate', rate); // 更新存储的当前倍速
            });

            // 添加到菜单
            menu.appendChild(newRateNode);
        });

        // 如果存在存储的倍速值,则设置播放器的倍速
        if (currentRate !== undefined && domVideoElement) {
            domVideoElement.playbackRate = currentRate;
        }

        // 监听快捷键
        document.addEventListener('keydown', (event) => {
            const pressedKey = event.key.toLowerCase();
            const { ctrlKey, altKey, shiftKey } = event;
            const controlKey = ctrlKey ? 'ctrl+' : '';
            const altKeyStr = altKey ? 'alt+' : '';
            const shiftKeyStr = shiftKey ? 'shift+' : '';

            customRates.forEach(({ rate, shortcut }) => {
                if (shortcut && `${controlKey}${altKeyStr}${shiftKeyStr}${shortcut}` === pressedKey) {
                    domVideoElement.playbackRate = rate;
                    GM_setValue('currentRate', rate); // 更新存储的当前倍速
                }
            });
        });
    });
})();

QingJ © 2025

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