YouTube Quick Speed Interface

Modify the YouTube HTML player interface

目前為 2023-07-21 提交的版本,檢視 最新版本

// ==UserScript==
// @name         YouTube Quick Speed Interface
// @name:en      YouTube Quick Speed Interface
// @namespace    https://twitter.com/CobleeH
// @version      1.05
// @description  Modify the YouTube HTML player interface
// @description:en Modify the YouTube HTML player interface
// @author       CobleeH
// @match        https://www.youtube.com/*
// @grant        none
// @license MIT
// ==/UserScript==

// @changelog    Version 1.05:
// - Added MutationObserver to dynamically add speed options when the YouTube player container is added to the document.
// - Fixed a bug where duplicate speed adjustment options were created by checking for the existence of the .ytp-speed-options element before adding.


(function() {
    'use strict';

    // Write your script code here

    // 添加倍速选项 (Add speed options)
    function addSpeedOptions() {
        // 检查是否已添加倍速选项 (Check if speed options are already added)
        if (document.querySelector('.ytp-speed-options')) {
            return;
        }

        var rightControls = document.querySelector('.ytp-right-controls');
        var leftControls = document.querySelector('.ytp-left-controls');
        if (rightControls && leftControls) {
            var speedOptions = document.createElement('div');
            speedOptions.classList.add('ytp-speed-options');
            speedOptions.style.display = 'flex';
            speedOptions.style.alignItems = 'center';
            speedOptions.style.order = '2';
            speedOptions.style.marginLeft = '10px';
            speedOptions.style.marginRight = '25px';

            var label = document.createElement('span');
            label.innerText = 'Speed';
            speedOptions.appendChild(label);

            var speeds = [0.5, 1, 1.5, 2];
            speeds.forEach(function(speed) {
                var option = document.createElement('div');
                option.innerText = speed + 'x';
                option.classList.add('ytp-speed-option');
                option.style.cursor = 'pointer';
                option.style.marginLeft = '5px';
                option.addEventListener('click', function() {
                    var player = document.querySelector('.video-stream');
                    if (player) {
                        player.playbackRate = speed;
                    }
                    highlightOption(option);
                });

                option.addEventListener('mouseover', function() {
                    option.classList.add('highlighted');
                });

                option.addEventListener('mouseout', function() {
                    if (!option.classList.contains('active')) {
                        option.classList.remove('highlighted');
                    }
                });

                speedOptions.appendChild(option);

                // 检查当前播放速度,如果与选项匹配,则添加高亮样式 (Check current playback speed and add highlight style if it matches the option)
                var player = document.querySelector('.video-stream');
                if (player && player.playbackRate === speed) {
                    highlightOption(option);
                }
            });

            leftControls.style.order = '1';
            rightControls.style.order = '3';

            document.querySelector('.ytp-chrome-controls').appendChild(speedOptions);
        }
    }

    function highlightOption(option) {
        // 移除其他选项的高亮样式 (Remove highlight style from other options)
        var options = document.querySelectorAll('.ytp-speed-option');
        options.forEach(function(opt) {
            opt.classList.remove('active');
        });

        // 添加当前选项的高亮样式 (Add highlight style to the current option)
        option.classList.add('active');
    }

    // 添加高亮样式 (Add highlight styles)
    var style = document.createElement('style');
    style.innerHTML = `
        .ytp-speed-option {
            transition: background-color 0.3s;
        }

        .ytp-speed-option:hover {
            background-color: rgba(211, 211, 211, 0.4);
        }

        .ytp-speed-option.active,
        .ytp-speed-option.active:hover {
            background-color: rgba(211, 211, 211, 0.4);
        }
    `;
    document.head.appendChild(style);

    // 使用 MutationObserver 监听播放器容器元素的变化 (Use MutationObserver to monitor changes in the player container element)
    var observer = new MutationObserver(function(mutations) {
        mutations.forEach(function(mutation) {
            // 检查是否有新添加的节点 (Check if there are newly added nodes)
            if (mutation.addedNodes) {
                Array.from(mutation.addedNodes).forEach(function(node) {
                    if (node.classList && node.classList.contains('html5-video-player')) {
                        // 播放器容器元素变化时添加倍速选项 (Add speed options when the player container element changes)
                        addSpeedOptions();
                    }
                });
            }
        });
    });

    // 观察整个文档的变化 (Observe changes in the entire document)
    observer.observe(document.documentElement, { childList: true, subtree: true });

    // 页面完全加载后添加倍速选项 (Add speed options when the page is fully loaded)
    addSpeedOptions();
})();


/*
================ Chinese Version =================

在YouTube播放器上添加倍速选项

此脚本将在YouTube HTML播放器界面中添加一个倍速控制功能,允许您使用预定义的倍速选项更改视频的播放速度。

使用说明:
- 安装脚本后,您将在播放器控制栏旁边看到一个“Speed”标签。
- 单击所需的速度选项以更改播放速度。
- 选定的速度选项将突出显示,并在视频播放时应用。

注意:
- 此脚本适用于YouTube的HTML播放器界面。

================ English Version ================

Adding Playback Speed Options to YouTube Player

This script adds a playback speed control feature to the YouTube HTML player interface, allowing you to change the video's playback speed using predefined speed options.

Instructions:
- After installing the script, you will see a "Speed" label next to the player controls.
- Click on the desired speed option to change the playback speed.
- The selected speed option will be highlighted and applied during video playback.

Note:
- This script is compatible with YouTube's HTML player interface.
*/

QingJ © 2025

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