YouTube 按键加速播放

在YouTube上按住右箭头键时视频加速到2.5倍速,避免与快进功能冲突

13.12.2024 itibariyledir. En son verisyonu görün.

Bu betiği kurabilmeniz için Tampermonkey, Greasemonkey ya da Violentmonkey gibi bir kullanıcı betiği eklentisini kurmanız gerekmektedir.

You will need to install an extension such as Tampermonkey to install this script.

Bu betiği kurabilmeniz için Tampermonkey ya da Violentmonkey gibi bir kullanıcı betiği eklentisini kurmanız gerekmektedir.

Bu betiği kurabilmeniz için Tampermonkey ya da Userscripts gibi bir kullanıcı betiği eklentisini kurmanız gerekmektedir.

You will need to install an extension such as Tampermonkey to install this script.

Bu komut dosyasını yüklemek için bir kullanıcı komut dosyası yöneticisi uzantısı yüklemeniz gerekecek.

(Zaten bir kullanıcı komut dosyası yöneticim var, kurmama izin verin!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Zateb bir user-style yöneticim var, yükleyeyim!)

// ==UserScript==
// @name         YouTube 按键加速播放
// @name:en      YouTube Speed Control
// @namespace    http://tampermonkey.net/
// @version      0.4
// @description  在YouTube上按住右箭头键时视频加速到2.5倍速,避免与快进功能冲突
// @description:en  Hold right arrow key to speed up YouTube video to 2.5x, without interfering with the forward function
// @author       landrarwolf
// @match        https://www.youtube.com/*
// @license      MIT
// @supportURL   https://github.com/yourusername/youtube-speed-control/issues
// @grant        none
// ==/UserScript==

(function() {
    'use strict';
    
    let normalSpeed = 1.0;  // 保存正常播放速度
    let speedTimeout = null; // 用于延迟处理速度变化
    let isSpeedUp = false;  // 标记是否处于加速状态
    let pressStartTime = 0;  // 记录按键开始时间
    let speedIndicator = null; // 速度提示元素
    
    // 创建速度提示元素
    function createSpeedIndicator() {
        const indicator = document.createElement('div');
        indicator.style.cssText = `
            position: fixed;
            top: 20px;
            left: 50%;
            transform: translateX(-50%);
            background-color: rgba(0, 0, 0, 0.8);
            color: white;
            padding: 8px 16px;
            border-radius: 4px;
            z-index: 9999;
            font-size: 14px;
            font-family: Arial, sans-serif;
            display: none;
            transition: opacity 0.2s;
        `;
        indicator.textContent = '⚡ 2.5x 加速中';
        document.body.appendChild(indicator);
        return indicator;
    }
    
    // 显示速度提示
    function showSpeedIndicator() {
        if (!speedIndicator) {
            speedIndicator = createSpeedIndicator();
        }
        speedIndicator.style.display = 'block';
        speedIndicator.style.opacity = '1';
    }
    
    // 隐藏速度提示
    function hideSpeedIndicator() {
        if (speedIndicator) {
            speedIndicator.style.opacity = '0';
            setTimeout(() => {
                speedIndicator.style.display = 'none';
            }, 200);
        }
    }

    // 监听键盘按下事件
    document.addEventListener('keydown', function(event) {
        if (event.key === 'ArrowRight') {
            if (!event.repeat) {
                pressStartTime = Date.now();
                speedTimeout = setTimeout(() => {
                    const video = document.querySelector('video');
                    if (video) {
                        normalSpeed = video.playbackRate;
                        video.playbackRate = 2.5;
                        isSpeedUp = true;
                        showSpeedIndicator();
                    }
                    event.preventDefault();
                    event.stopPropagation();
                }, 200);
            } else {
                if (Date.now() - pressStartTime > 200) {
                    event.preventDefault();
                    event.stopPropagation();
                }
            }
        }
    }, true);
    
    // 监听键盘释放事件
    document.addEventListener('keyup', function(event) {
        if (event.key === 'ArrowRight') {
            const pressDuration = Date.now() - pressStartTime;
            
            if (speedTimeout) {
                clearTimeout(speedTimeout);
                speedTimeout = null;
            }
            
            if (pressDuration < 200) {
                return;
            }
            
            if (isSpeedUp) {
                const video = document.querySelector('video');
                if (video) {
                    video.playbackRate = normalSpeed;
                    isSpeedUp = false;
                    hideSpeedIndicator();
                }
                event.preventDefault();
                event.stopPropagation();
            }
        }
    }, true);
})();