Greasy Fork

Greasy Fork is available in English.

自定义哔哩哔哩视频播放速度,记住播放速度、位置

可以使用按键 z(恢复1倍速)、x(减0.1倍速)、c(加0.1倍速),s(记录播放速度,下次打开视频直接使用记录的播放速度),d(删除记录的播放速度),

当前为 2021-06-16 提交的版本,查看 最新版本

// ==UserScript==
// @name         自定义哔哩哔哩视频播放速度,记住播放速度、位置
// @namespace    http://tampermonkey.net/
// @version      0.1.7
// @description  可以使用按键 z(恢复1倍速)、x(减0.1倍速)、c(加0.1倍速),s(记录播放速度,下次打开视频直接使用记录的播放速度),d(删除记录的播放速度),
// @author       felix
// @icon         chrome://favicon/http://www.bilibili.com/
// @match        https://www.bilibili.com/video/*
// @grant        GM_registerMenuCommand
// @grant        GM_unregisterMenuCommand
// ==/UserScript==
(function () {
    'use strict';

    // ===================================================配置区=====================================================================================
    var STORAGE_KEY = {
        BILIBILI_VIDEO_SPEED_SIWTCH: "bilibili_video_speed_switch",
        BILIBILI_VIDEO_SPEED: "bilibili_video_speed"
    };

    var SETTING = {
        STEP_SIZE: 0.1,
        MAX_SPEED: 5,
        MIN_SPEED: 0.1,
        REMEMBER_SPEED_MENU_ID: null,
    };

    // ===================================================加载区=====================================================================================
    var autoAddInterval = setInterval(loading, 3000);

    function loading() {
        clearInterval(autoAddInterval);
        addToast();
        addButton();
        loadSpeed();
    }

    // 添加按钮
    function addButton() {
        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>';
        document.getElementById("arc_toolbar_report").appendChild(div);
        document.getElementById("reduce").onclick = function () { reduceSpeed(); };
        document.getElementById("add").onclick = function () { addSpeed(); };
    }

    function addToast() {
        var div = document.createElement("div");
        div.innerHTML = `
            <div class="bilibili-player-volumeHint felix" style="opacity: 100; display: none; ">
                <span class="bilibili-player-volumeHint-icon video-state-volume-max">
                    <i class="bilibili-player-iconfont bilibili-player-iconfont-volume icon-24soundsmall"></i>
                    <i class="bilibili-player-iconfont bilibili-player-iconfont-volume-max icon-24soundlarge"></i>
                    <i class="bilibili-player-iconfont bilibili-player-iconfont-volume-min icon-24soundoff"></i>
                </span>
                <span class="bilibili-player-volumeHint-text">felix</span>
            </div>
        `;

        $(".bilibili-player-video-wrap").append(div);
    }

    // 加载播放速度
    function loadSpeed() {
        var speed = localStorage.getItem(STORAGE_KEY.BILIBILI_VIDEO_SPEED);
        if (speed) {
            changeSpeed(speed);
            loadRemoveSpeedMenu();
        } else {
            loadSaveSpeedMenu();
        }
    }

    // ===================================================获取控件区=====================================================================================
    // 获取 video控件
    function getVideo() {
        return document.querySelector('video');
    }

    // ===================================================键盘监听区=====================================================================================
    document.onkeydown = function (e) {
        if (e.target.nodeName !== 'BODY') return;
        if (/^[zxcZXC]$/.test(e.key)) {
            if (e.key === 'z' || e.key === 'Z') changeSpeed(1);
            if (e.key === 'x' || e.key === 'X') reduceSpeed();
            if (e.key === 'c' || e.key === 'C') addSpeed();
        }
    };


    // ==================================================播放速度=================================================================================
    function loadSaveSpeedMenu() {
        if (SETTING.REMEMBER_SPEED_MENU_ID) GM_unregisterMenuCommand(SETTING.REMEMBER_SPEED_MENU_ID);
        SETTING.REMEMBER_SPEED_MENU_ID = GM_registerMenuCommand("记住播放速度", openSaveSpeedSitch);
    }

    function loadRemoveSpeedMenu() {
        if (SETTING.REMEMBER_SPEED_MENU_ID) GM_unregisterMenuCommand(SETTING.REMEMBER_SPEED_MENU_ID);
        SETTING.REMEMBER_SPEED_MENU_ID = GM_registerMenuCommand("忘记播放速度", closeSaveSpeedSitch);
    }

    function openSaveSpeedSitch() {
        localStorage.setItem(STORAGE_KEY.BILIBILI_VIDEO_SPEED_SIWTCH, true);
        setSpeedToStorage();
    }

    function closeSaveSpeedSitch() {
        localStorage.setItem(STORAGE_KEY.BILIBILI_VIDEO_SPEED_SIWTCH, false);
        removeSpeedFromStorage();
    }

    // 保存播放速度到localStorage
    function setSpeedToStorage() {
        localStorage.setItem(STORAGE_KEY.BILIBILI_VIDEO_SPEED, getVideoSpeed());
        loadRemoveSpeedMenu();
    }

    // 从localStorage中删除保存的播放速度
    function removeSpeedFromStorage() {
        localStorage.removeItem(STORAGE_KEY.BILIBILI_VIDEO_SPEED);
        loadSaveSpeedMenu();
    }

    // 获取当前播放速度
    function getVideoSpeed() {
        return getVideo().playbackRate;
    }

    // 减速
    function reduceSpeed(stepSize) {
        if (!stepSize) stepSize = SETTING.STEP_SIZE;
        var playSpeed = Number((Number(document.getElementById("speed").innerText) * 10 - stepSize * 10) / 10).toFixed(1);
        changeSpeed(playSpeed);
        $(".bilibili-player-volumeHint.felix").attr("style", '{ "opacity": 100, "display": "block" }')
        $(".bilibili-player-volumeHint.felix .bilibili-player-volumeHint-text").text(playSpeed)
        removeSpeedToast();
    }

    // 加速
    function addSpeed(stepSize) {
        if (!stepSize) stepSize = SETTING.STEP_SIZE;
        var playSpeed = Number((Number(document.getElementById("speed").innerText) * 10 + stepSize * 10) / 10).toFixed(1);
        changeSpeed(playSpeed);
        $(".bilibili-player-volumeHint.felix").attr("style", '{ "opacity": 100, "display": "block" }')
        $(".bilibili-player-volumeHint.felix .bilibili-player-volumeHint-text").text(playSpeed)
        removeSpeedToast();
    }

    function removeSpeedToast() {
        var interval = setInterval(() => {
            var opacity = $(".bilibili-player-volumeHint.felix").css("opacity")
            opacity = opacity - 0.05;
            if (opacity <= 0.7) {
                $(".bilibili-player-volumeHint.felix").css("opacity", 0);
                $(".bilibili-player-volumeHint.felix").css("dispaly", "none");
                clearInterval(interval)
            } else {
                $(".bilibili-player-volumeHint.felix").css("opacity", opacity);
            }
        }, 200);
    }

    // 改变播放速度
    function changeSpeed(playSpeed) {
        if (playSpeed && playSpeed >= SETTING.MIN_SPEED && playSpeed <= SETTING.MAX_SPEED) {
            getVideo().playbackRate = playSpeed;
            document.getElementById("speed").innerText = playSpeed;
            var saveSpeedswitch = localStorage.getItem(STORAGE_KEY.BILIBILI_VIDEO_SPEED_SIWTCH);
            if (saveSpeedswitch) setSpeedToStorage();
        }
    }
})();