All Media Speed

Use Alt + . and Alt + , to change speed by 0.1x. Overlay is styled like

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         All Media Speed
// @namespace    http://tampermonkey.net/
// @version      2.0
// @description  Use Alt + . and Alt + , to change speed by 0.1x. Overlay is styled like
// YouTube's (centered, small, subtle). Precision to 2 decimals, speed persists across reloads. Works on all videos, even dynamic ones.
// @author       GPT
// @match        *://*/*
// @grant        none
// @license     CC-BY-NC-4.0
// ==/UserScript==

(function () {
    'use strict';

    const STEP = 0.1;
    const MIN = 0.1;
    const MAX = 5.0;
    const STORAGE_KEY = 'videoSpeedPersistent:';
    let speed = loadSpeed();

    // --- Overlay styled like YouTube ---
    const overlay = document.createElement('div');
    Object.assign(overlay.style, {
        position: 'fixed',
        top: '50%',
        left: '50%',
        transform: 'translate(-50%, -50%)',
        backgroundColor: 'rgba(0, 0, 0, 0.8)',
        color: '#fff',
        padding: '8px 16px',
        fontSize: '14px',
        fontWeight: '500',
        fontFamily: 'Arial, sans-serif',
        borderRadius: '4px',
        zIndex: '999999',
        pointerEvents: 'none',
        opacity: '0',
        transition: 'opacity 0.5s ease',
    });
    document.body.appendChild(overlay);

    let timeout;

    function round2(n) {
        return Math.round(n * 100) / 100;
    }

    function showOverlay(msg) {
        clearTimeout(timeout);
        overlay.textContent = msg;
        overlay.style.opacity = '1';
        timeout = setTimeout(() => {
            overlay.style.opacity = '0';
        }, 1500);
    }

    function saveSpeed(val) {
        localStorage.setItem(STORAGE_KEY + location.hostname, val.toFixed(2));
    }

    function loadSpeed() {
        const raw = localStorage.getItem(STORAGE_KEY + location.hostname);
        const val = parseFloat(raw);
        return !isNaN(val) ? round2(val) : 1.0;
    }

    function setSpeed(val) {
        val = Math.max(MIN, Math.min(MAX, round2(val)));
        speed = val;
        document.querySelectorAll('video').forEach(v => v.playbackRate = speed);
        saveSpeed(speed);
        showOverlay(`Speed: ${speed.toFixed(2)}x`);
    }

    function applyToNewVideos() {
        document.querySelectorAll('video').forEach(v => {
            v.playbackRate = speed;
        });
    }

    window.addEventListener('keydown', (e) => {
        if (e.altKey && !e.shiftKey && !e.ctrlKey && !e.metaKey) {
            if (e.key === '.') {
                e.preventDefault();
                setSpeed(speed + STEP);
            } else if (e.key === ',') {
                e.preventDefault();
                setSpeed(speed - STEP);
            }
        }
    });

    window.addEventListener('DOMContentLoaded', () => {
        setSpeed(speed);
    });

    new MutationObserver(applyToNewVideos).observe(document.body, {
        childList: true,
        subtree: true
    });
})();