YouTube 4x Speed

Add a 4x speed option to YouTube player.

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         YouTube 4x Speed
// @namespace    https://www.youtube.com/
// @version      1.0
// @description  Add a 4x speed option to YouTube player.
// @author       YourName
// @match        https://www.youtube.com/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    function addSpeedOption() {
        const player = document.querySelector('video');
        const speedMenu = document.querySelector('.ytp-settings-menu');
        
        if (player && speedMenu) {
            const existingSpeeds = player.playbackRate;
            
            // Add the 4x speed option to the playback rate menu if it doesn't already exist
            if (!Array.from(document.querySelectorAll('.ytp-menuitem')).some(el => el.innerText.includes('4'))) {
                const speedOption = document.createElement('div');
                speedOption.className = 'ytp-menuitem';
                speedOption.setAttribute('role', 'menuitem');
                speedOption.setAttribute('aria-checked', 'false');
                speedOption.setAttribute('tabindex', '0');
                
                const speedLabel = document.createElement('div');
                speedLabel.className = 'ytp-menuitem-label';
                speedLabel.innerText = '4x';
                
                speedOption.appendChild(speedLabel);
                
                speedOption.addEventListener('click', function() {
                    player.playbackRate = 4.0;
                    closeSpeedMenu();
                });

                speedMenu.appendChild(speedOption);
            }
        }
    }

    function closeSpeedMenu() {
        // Close the YouTube settings menu
        const settingsButton = document.querySelector('.ytp-settings-button');
        settingsButton.click();
    }

    function observerCallback() {
        addSpeedOption();
    }

    // Mutation observer to ensure the script runs even after navigating to a different video
    const observer = new MutationObserver(observerCallback);
    observer.observe(document, { childList: true, subtree: true });
})();