Playback Speed Control

Add a custom control for changing YouTube video playback speed

目前为 2024-08-25 提交的版本。查看 最新版本

// ==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();
})();