YouTube Speed-Adjusted Time Display

Shows speed-adjusted time for YouTube videos

Stan na 18-11-2024. Zobacz najnowsza wersja.

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Greasemonkey lub Violentmonkey.

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana będzie instalacja rozszerzenia Tampermonkey lub Userscripts.

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

Aby zainstalować ten skrypt, musisz zainstalować rozszerzenie menedżera skryptów użytkownika.

(Mam już menedżera skryptów użytkownika, pozwól mi to zainstalować!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Musisz zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

(Mam już menedżera stylów użytkownika, pozwól mi to zainstalować!)

// ==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);
})();