YouTube Speed-Adjusted Time Display

Shows speed-adjusted time for YouTube videos

Tính đến 18-11-2024. Xem phiên bản mới nhất.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

Bạn sẽ cần cài đặt một tiện ích mở rộng như Tampermonkey hoặc Violentmonkey để cài đặt kịch bản này.

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

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

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

You will need to install a user script manager extension to install this script.

(Tôi đã có Trình quản lý tập lệnh người dùng, hãy cài đặt nó!)

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.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

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