Video Speed Controller

Change video playback speed with hotkeys.

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

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

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

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

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

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

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

Advertisement:

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

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

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

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

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

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

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

Advertisement:

// ==UserScript==
// @name           Video Speed Controller
// @icon           https://www.favicon.cc/favicon/39/699/favicon.png
// @namespace
// @version        1.0
// @description    Change video playback speed with hotkeys.
// @author         BlueAG
// @license        MIT
// @match          *://*/*
// @grant          none
// @run-at         document-idle
// @namespace https://greasyfork.org/users/1452677
// ==/UserScript==

(function() {
    'use strict';

    // Speed step for increase/decrease
    const SPEED_STEP = 0.25;
    const MIN_SPEED = 0.25;
    const MAX_SPEED = 10.0;

    // Show toast notification
    function showToast(text) {
        let toast = document.getElementById('tm-speed-toast');
        if (!toast) {
            toast = document.createElement('div');
            toast.id = 'tm-speed-toast';
            toast.style.cssText = `
                position: fixed;
                top: 20px;
                right: 20px;
                background: rgba(0,0,0,0.8);
                color: #fff;
                padding: 10px 16px;
                border-radius: 8px;
                font-family: Arial, sans-serif;
                font-size: 16px;
                font-weight: bold;
                z-index: 999999;
                transition: opacity 0.3s;
                pointer-events: none;
            `;
            document.body.appendChild(toast);
        }
        toast.textContent = text;
        toast.style.opacity = '1';
        clearTimeout(toast._timeout);
        toast._timeout = setTimeout(() => toast.style.opacity = '0', 1000);
    }

    // Get all video elements on page
    function getVideos() {
        return Array.from(document.querySelectorAll('video, audio'));
    }

    // Set speed for all videos
    function setSpeed(speed) {
        const videos = getVideos();
        if (videos.length === 0) return;

        // Clamp speed
        speed = Math.max(MIN_SPEED, Math.min(MAX_SPEED, speed));

        videos.forEach(v => {
            v.playbackRate = speed;
        });

        showToast(`${speed.toFixed(2)}x`);
    }

    // Adjust speed by delta
    function adjustSpeed(delta) {
        const videos = getVideos();
        if (videos.length === 0) return;

        const currentSpeed = videos[0].playbackRate;
        setSpeed(currentSpeed + delta);
    }

    // Reset speed to 1.0x
    function resetSpeed() {
        setSpeed(1.0);
    }

    // Hotkey listener
    document.addEventListener('keydown', (e) => {
        // Ignore if typing in input/textarea
        if (e.target.matches('input, textarea, [contenteditable="true"]')) return;

        switch(e.key) {
            case ']': // increase speed
            case ']':
                adjustSpeed(SPEED_STEP);
                e.preventDefault();
                break;

            case '[': // decrease speed
            case '[':
                adjustSpeed(-SPEED_STEP);
                e.preventDefault();
                break;

            case '0': // reset to 1.0x
                resetSpeed();
                e.preventDefault();
                break;


			case 'p': // pause/play toggle
			case 'P':
				getVideos().forEach(v => {
					if (v.paused) {
					v.play();
					showToast('▶ Play');
					} else {
					v.pause();
					showToast('⏸ Pause');
					}
				});
				e.preventDefault();
				break;

			case 'z': // mute/unmute toggle
			case 'Z':
                getVideos().forEach(v => {
                   v.muted = !v.muted;
                   showToast(v.muted ? '🔇 Muted' : '🔊 Unmuted');
                });
                e.preventDefault();
                break;
		}
    });

    // Apply 1.0x to new videos that load later
    const observer = new MutationObserver(() => {
        // Don't auto-set here or it'll override user changes
        // But we could show current speed if needed
    });

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

})();