YouTube Float Input Box for Playback Speed

Adds a float input box to adjust playback speed on YouTube after 2 seconds of user input

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

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

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

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

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

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

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         YouTube Float Input Box for Playback Speed
// @namespace    http://tampermonkey.net/
// @version      2024-10-17
// @description  Adds a float input box to adjust playback speed on YouTube after 2 seconds of user input
// @author       CigiMundus
// @license MIT
// @icon         https://img.icons8.com/?size=96&id=hAc8LzPCcNEX&format=png
// @match        https://www.youtube.com/*
// ==/UserScript==

(function() {
    'use strict';

    let playbackTimer;
    const defaultSpeed = 1.064;

    function addFloatInputBox() {
        const likeButtonContainer = document.querySelector('#top-level-buttons-computed');

        if (likeButtonContainer && !document.querySelector('.custom-float-input')) {
            const floatInput = document.createElement('input');
            floatInput.type = 'number';
            floatInput.step = '0.1';
            floatInput.min = '0.1';
            floatInput.max = '2';
            floatInput.value = defaultSpeed;
            floatInput.placeholder = 'Speed (0.1 - 2)';
            floatInput.className = 'custom-float-input';

            floatInput.style.padding = '8px 12px';
            floatInput.style.borderRadius = '12px';
            floatInput.style.border = '2px solid #00796b';
            floatInput.style.backgroundColor = '#e0f2f1';
            floatInput.style.color = '#00796b';
            floatInput.style.fontSize = '14px';
            floatInput.style.marginRight = '10px';
            floatInput.style.outline = 'none';
            floatInput.style.transition = 'border 0.3s, box-shadow 0.3s';

            floatInput.addEventListener('focus', () => {
                floatInput.style.border = '2px solid #004d40';
                floatInput.style.boxShadow = '0 0 8px rgba(0, 77, 64, 0.4)';
            });

            floatInput.addEventListener('blur', () => {
                floatInput.style.border = '2px solid #00796b';
                floatInput.style.boxShadow = 'none';
            });

            floatInput.addEventListener('input', (event) => {
                let newSpeed = parseFloat(event.target.value);
                if (isNaN(newSpeed) || newSpeed < 0.1) {
                    newSpeed = 0.1;
                    floatInput.value = newSpeed;
                } else if (newSpeed > 2) {
                    newSpeed = 2;
                    floatInput.value = newSpeed;
                }

                if (playbackTimer) {
                    clearTimeout(playbackTimer);
                }

                playbackTimer = setTimeout(() => {
                    const video = document.querySelector('video');
                    if (video) {
                        video.playbackRate = newSpeed;
                        console.log(`Video playback speed set to: ${newSpeed}`);
                    } else {
                        console.error('No video element found.');
                    }
                }, 2000);
            });

            likeButtonContainer.insertBefore(floatInput, likeButtonContainer.firstChild);

            const video = document.querySelector('video');
            if (video) {
                video.playbackRate = defaultSpeed;
                console.log(`Video playback speed set to default: ${defaultSpeed}`);
            }
        }
    }

    const observer = new MutationObserver((mutations) => {
        mutations.forEach(() => {
            addFloatInputBox();
        });
    });

    observer.observe(document.body, { childList: true, subtree: true });

    addFloatInputBox();
})();