YouTube Speed-Adjusted Time Display

Shows speed-adjusted time for YouTube videos

Per 18-11-2024. Zie de nieuwste versie.

Voor het installeren van scripts heb je een extensie nodig, zoals Tampermonkey, Greasemonkey of Violentmonkey.

Voor het installeren van scripts heb je een extensie nodig, zoals Tampermonkey of Violentmonkey.

Voor het installeren van scripts heb je een extensie nodig, zoals Tampermonkey of Violentmonkey.

Voor het installeren van scripts heb je een extensie nodig, zoals Tampermonkey of Userscripts.

Voor het installeren van scripts heb je een extensie nodig, zoals {tampermonkey_link:Tampermonkey}.

Voor het installeren van scripts heb je een gebruikersscriptbeheerder nodig.

(Ik heb al een user script manager, laat me het downloaden!)

Voor het installeren van gebruikersstijlen heb je een extensie nodig, zoals {stylus_link:Stylus}.

Voor het installeren van gebruikersstijlen heb je een extensie nodig, zoals {stylus_link:Stylus}.

Voor het installeren van gebruikersstijlen heb je een extensie nodig, zoals {stylus_link:Stylus}.

Voor het installeren van gebruikersstijlen heb je een gebruikersstijlbeheerder nodig.

Voor het installeren van gebruikersstijlen heb je een gebruikersstijlbeheerder nodig.

Voor het installeren van gebruikersstijlen heb je een gebruikersstijlbeheerder nodig.

(Ik heb al een beheerder - laat me doorgaan met de installatie!)

// ==UserScript==
// @name         YouTube Speed-Adjusted Time Display
// @namespace    http://tampermonkey.net/
// @version      1.1
// @description  Shows speed-adjusted time for YouTube videos
// @author       kavinned
// @match        https://www.youtube.com/*
// @grant        none
// @icon         https://www.google.com/s2/favicons?sz=64&domain=YouTube.com
// @license      MIT
// ==/UserScript==
(function() {
    'use strict';
    function updateTimeDisplay() {
        const video = document.querySelector('video');
        if (!video) return;
        const currentTimeDisplay = document.querySelector('.ytp-time-current');
        const durationDisplay = document.querySelector('.ytp-time-duration');
        const remainingTimeDisplay = document.querySelector('.speed-adjusted-time');
        if (!currentTimeDisplay || !durationDisplay || !remainingTimeDisplay) return;

        const currentTime = video.currentTime;
        const duration = video.duration;
        const playbackRate = video.playbackRate;

        const adjustedCurrentTime = currentTime / playbackRate;
        const adjustedDuration = duration / playbackRate;
        const remainingTime = adjustedDuration - adjustedCurrentTime;

        function formatTime(seconds) {
            const hours = Math.floor(seconds / 3600);
            const minutes = Math.floor((seconds % 3600) / 60);
            const secs = Math.floor(seconds % 60);
            if (hours > 0) {
                return `${hours}:${minutes.toString().padStart(2, '0')}:${secs.toString().padStart(2, '0')}`;
            } else {
                return `${minutes}:${secs.toString().padStart(2, '0')}`;
            }
        }

        currentTimeDisplay.textContent = formatTime(adjustedCurrentTime);
        durationDisplay.textContent = formatTime(adjustedDuration);
        remainingTimeDisplay.textContent = `(-${formatTime(remainingTime)})`;
    }

    function createSpeedTimeDisplay() {
        const timeContainer = document.querySelector('.ytp-time-wrapper');
        if (!timeContainer || document.querySelector('.speed-adjusted-time')) return;

        const speedTimeDiv = document.createElement('div');
        speedTimeDiv.className = 'speed-adjusted-time ytp-time-display';
        speedTimeDiv.style.color = 'inherit';
        speedTimeDiv.style.fontSize = 'inherit';

        timeContainer.append(speedTimeDiv);
    }

    const observer = new MutationObserver((mutations) => {
        if (document.querySelector('video')) {
            createSpeedTimeDisplay();
            updateTimeDisplay();
        }
    });

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

    setInterval(updateTimeDisplay, 1000);
    document.addEventListener('ratechange', updateTimeDisplay, true);
})();