Greasy Fork

Greasy Fork is available in English.

自定义哔哩哔哩视频播放速度

可以使用按键 z(恢复1倍速)、x(减0.1)、c(加0.1)调整倍速,支持0.1-5倍速

当前为 2021-04-08 提交的版本,查看 最新版本

// ==UserScript==
// @name         自定义哔哩哔哩视频播放速度
// @namespace    http://tampermonkey.net/
// @version      0.1.1
// @description  可以使用按键 z(恢复1倍速)、x(减0.1)、c(加0.1)调整倍速,支持0.1-5倍速
// @author       felix
// @match        https://www.bilibili.com/video/*
// @grant        none
// ==/UserScript==
(function () {
    'use strict';

    var Setting = {
        stepSize: 0.1,
        maxSpeed: 5,
        minSpeed: 0.1
    };

    var autoAddInterval = setInterval(addButton, 2000);

    function addButton() {
        clearInterval(autoAddInterval);
        var viewbox_report = document.getElementById("arc_toolbar_report");
        var div = document.createElement("div");
        div.innerHTML = '<button id="reduce" style="width:15px;margin:0 3px">-</button><button style="width:30px"><sapn id="speed">1<span/></button><button id="add" style="width:15px;margin:0 3px">+</button>';
        viewbox_report.appendChild(div);

        document.getElementById("reduce").onclick = function () {
            reduceSpeed();
        };

        document.getElementById("add").onclick = function () {
            addSpeed();
        };
    }

    function reduceSpeed(stepSize) {
        if (!stepSize) {
            stepSize = Setting.stepSize;
        }
        var speedSpan = document.getElementById("speed");
        var playSpeed = Number(Number(Number(speedSpan.innerText) * 10 - stepSize * 10) / 10).toFixed(1);
        changeSpeed(playSpeed);
    }

    function addSpeed(stepSize) {
        if (!stepSize) {
            stepSize = Setting.stepSize;
        }
        var speedSpan = document.getElementById("speed");
        var playSpeed = Number(Number(Number(speedSpan.innerText) * 10 + stepSize * 10) / 10).toFixed(1);
        changeSpeed(playSpeed);
    }

    function changeSpeed(playSpeed) {
        if (playSpeed && playSpeed >= Setting.minSpeed && playSpeed <= Setting.maxSpeed) {
            document.querySelector('video').playbackRate = playSpeed;
            var speedSpan = document.getElementById("speed");
            speedSpan.innerText = playSpeed;
        }
    }

    // 键盘快捷键
    document.onkeydown = function (e) {
        if (e.target.nodeName !== 'BODY') return;
        if (/^[zxc]$/.test(e.key)) {
            if (e.key === 'z') {
                changeSpeed(1);
            }
            if (e.key === 'x') {
                reduceSpeed();
            }
            if (e.key === 'c') {
                addSpeed();
            }
        }
    };
})();