Playback Speed Control

Add a custom control for changing YouTube video playback speed

当前为 2024-08-25 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Playback Speed Control
// @namespace    https://github.com/yourusername/YouTubePlaybackSpeed
// @version      1.2
// @description  Add a custom control for changing YouTube video playback speed
// @author       Patrick
// @match        *://*.youtube.com/*
// @grant        none
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    // Function to add a playback speed control to the YouTube player
    function addSpeedControl() {
        // Check if the control is already present
        if (document.querySelector('#custom-speed-control')) return;

        // Create the button for speed control
        const controlButton = document.createElement('button');
        controlButton.id = 'custom-speed-control';
        controlButton.textContent = 'Speed: 1x';
        controlButton.style.position = 'absolute';
        controlButton.style.bottom = '10px';
        controlButton.style.right = '10px';
        controlButton.style.backgroundColor = 'rgba(0,0,0,0.7)';
        controlButton.style.color = '#fff';
        controlButton.style.border = 'none';
        controlButton.style.padding = '5px 10px';
        controlButton.style.borderRadius = '3px';
        controlButton.style.cursor = 'pointer';
        controlButton.style.zIndex = '9999';

        // Append the button to the player controls
        const controls = document.querySelector('.ytp-chrome-bottom');
        if (controls) {
            controls.appendChild(controlButton);

            // Add event listener to change speed
            controlButton.addEventListener('click', () => {
                const video = document.querySelector('video');
                if (!video) return;

                let currentSpeed = video.playbackRate;
                const speeds = [0.5, 1, 1.5, 2]; // Available speeds
                let nextSpeedIndex = (speeds.indexOf(currentSpeed) + 1) % speeds.length;
                let newSpeed = speeds[nextSpeedIndex];

                video.playbackRate = newSpeed;
            });
        }

        // Detect screen orientation changes
        window.addEventListener('orientationchange', () => {
            // Adjust playback speed here based on orientation (portrait or landscape)
            // You can customize this part according to your preference
        });
    }

    // Call the function to add the speed control
    addSpeedControl();
})();