Playback Speed Control

Add a custom control for changing YouTube video playback speed

目前為 2024-08-25 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 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();
})();