快捷键控制视频倍速、超级快进

(支持B站等)用快捷键(c, x, z)控制倍速播放,连按3次右箭头快进30s,连按4次则60s。具体而言:按 c 加速0.25,x 减速0.25,a 加速0.5,q减速0.5。腾讯视频、爱奇艺、百度网盘的快捷键只支持倍速播放,不支持快进。

// ==UserScript==
// @name          快捷键控制视频倍速、超级快进
// @version       1.0.0
// @description   (支持B站等)用快捷键(c, x, z)控制倍速播放,连按3次右箭头快进30s,连按4次则60s。具体而言:按 c 加速0.25,x 减速0.25,a 加速0.5,q减速0.5。腾讯视频、爱奇艺、百度网盘的快捷键只支持倍速播放,不支持快进。
// @author        ww
// @copyright     ww
// @license       MIT
// @match         *://www.bilibili.com/video/av*
// @match         *://www.bilibili.com/video/BV*
// @match         https://v.qq.com/x/cover/*
// @match         https://www.iqiyi.com/*
// @match         https://pan.baidu.com/pfile/video?path=*
// @grant         GM_addStyle
// @require       https://static.hdslb.com/js/jquery.min.js
// @namespace https://greasyfork.org/users/1034870
// ==/UserScript==
/* globals $ waitForKeyElements */
// @[ You can find all source codes in GitHub repo ]
!function() {
    "use strict";

    const duration = 1500;

    const url = window.location.href;
    let site = 0;
    if(url.indexOf("bilibili") > -1){
        site = 0;
    }else if(url.indexOf("qq.com") > -1){
        site = 1;
    }else if(url.indexOf("iqiyi.com") > -1){
        site = 2;
    }
    console.log("site: "+site);

    setTimeout(setRate, 1000);

    let lock = false;

    function setRate(){
        const video = document.querySelector('video');
        console.log("video: "+video);
        // 初始化倍速
        let playbackRate = 1.0;
        console.log("playbackRate: "+playbackRate);

        // 监听键盘事件
        document.addEventListener('keydown', (event) => {
            let adjustFlag = false;
            switch (event.key) {
                case 'z':
                    // 恢复正常速度
                    playbackRate = 1.0;
                    // adjustFlag = true;
                    break;
                case 'a':
                    playbackRate += 0.5;
                    adjustFlag = true;
                    break;
                case 'q':
                    playbackRate -= 0.5;
                    adjustFlag = true;
                    if (playbackRate < 0.25) {
                        playbackRate = 0.5; // 设置最低倍速
                    }
                    break;
                case 'c':
                    adjustFlag = true;
                    playbackRate += 0.25;
                    break;
                case 'x':
                    playbackRate -= 0.25;
                    adjustFlag = true;
                    if (playbackRate < 0.25) {
                        playbackRate = 0.25; // 最低倍速为0.25
                    }
                    break;
            }
            video.playbackRate = playbackRate;
            if(adjustFlag){
                if(site === 0){
                    const element = document.querySelector("#bilibili-player > div > div");
                    element.setAttribute("data-ctrl-hidden", "false");

                    // 按倍速键的计数器,按倍速键加1,duation时间后减1,并判断减完是否≤1,效果是最后一次按倍速键后才隐藏底部条。
                    let showCount = sessionStorage.getItem("show");
                    if(isEmpty(showCount)){
                        sessionStorage.setItem("show", 1);
                    }else{
                        sessionStorage.setItem("show", parseInt(showCount) + 1);
                    }

                    setTimeout(() => {
                        let showCount2 = sessionStorage.getItem("show");
                        if(showCount2 <= 1){
                            element.setAttribute("data-ctrl-hidden", "true");
                            sessionStorage.setItem("show", "");
                        }else{
                            sessionStorage.setItem("show", parseInt(showCount2) - 1);
                        }
                    }, duration);
                }
                if(site === 1){
                    const element = document.querySelector("#player > div.plugin_ctrl_txp_bottom");
                    element.classList.remove("txp_none");

                    setTimeout(() => {
                        element.classList.add("txp_none");
                    }, duration);
                }

                // box.style.display = "block";
                // box.textContent = `倍速: ${playbackRate}`;
                // clearTimeout(box.timeout);
                // box.timeout = setTimeout(() => {
                //     box.style.display = "none";
                // }, 1500);
            }

        });
    }

    function isEmpty(item){
        if(item===null || item===undefined || item.length===0){
            return true;
        }else{
            return false;
        }
    }
    // 快进、快退
    // 初始化变量
    let lastKeyPressTime = 0;
    let keyPressCount = 0;
    const doublePressThreshold = 300; // 两次按键的时间间隔阈值,单位毫秒

    // 监听键盘按键事件
    document.addEventListener('keydown', function (event) {
        const currentTime = new Date().getTime();

        // 检查按下的键是否是右箭头键或左箭头键
        if (event.key === 'ArrowRight' || event.key === 'ArrowLeft') {
            // 检查两次按键的时间间隔是否小于阈值
            if (currentTime - lastKeyPressTime < doublePressThreshold) {
                keyPressCount++; // 增加按键计数
            } else {
                keyPressCount = 1; // 重置按键计数
            }

            // 更新最后一次按键时间
            lastKeyPressTime = currentTime;

            // 获取视频元素
            const video = document.querySelector('video');
            if (video) {
                if (event.key === 'ArrowRight') {
                    // 右箭头键逻辑
                    if (keyPressCount === 3) {
                        video.currentTime += 15; // 快进30秒
                    } else if (keyPressCount === 4) {
                        video.currentTime += 25; // 快进1分钟,即:25(快进25秒)+30(触发按3次的30秒)+5(触发默认的5秒)
                        keyPressCount = 0; // 重置计数
                    }
                } else if (event.key === 'ArrowLeft') {
                    // 左箭头键逻辑
                    if (keyPressCount === 3) {
                        video.currentTime -= 15; // 快退30秒
                    } else if (keyPressCount === 4) {
                        video.currentTime -= 25; // 快退60秒
                        keyPressCount = 0; // 重置计数
                    }
                }
            }
        } else {
            // 如果按下其他键,重置计数
            keyPressCount = 0;
        }
    });

    function addCustomStyles() {
        var css = `
        .tool-box {
            display: none;
            position: absolute;
            bottom: 11rem;
            left: 50%;
            transform: translate(-50%, 0%);
            width: 5rem;
            background: rgba(0, 0, 0, 0.7);
            color: white;
            padding: 0.5rem;
            border-radius: 5px;
            font-size: 1rem;
            text-align: center;
            z-index: 99999   !important;
        }
    `;
        GM_addStyle(css);
    }
    addCustomStyles();

    var box=getBox();
    console.log(box);
    document.body.appendChild(box);

    function getBox(){
        var box = document.createElement("div");
        box.classList.add("tool-box");
        return box;
    }

}();