YouTube - Remaining time

Displays the remaining duration of a YouTube video next to the video duration, taking into account the playback rate.

目前為 2024-03-20 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name                          YouTube - Remaining time
// @namespace                 https://gist.github.com/4lrick/cf14cf267684f06c1b7bc559ddf2b943
// @version                       1.7
// @description                 Displays the remaining duration of a YouTube video next to the video duration, taking into account the playback rate.
// @description:fr              Affiche la durée restante d'une vidéo YouTube à côté de la durée de la vidéo, en tenant compte de la vitesse de lecture.
// @description:es             Muestra la duración restante de un video de YouTube junto a la duración del video, teniendo en cuenta la velocidad de reproducción.
// @description:de            Zeigt die verbleibende Dauer eines YouTube-Videos neben der Videodauer an und berücksichtigt dabei die Wiedergabegeschwindigkeit.
// @description:it              Mostra la durata rimanente di un video di YouTube accanto alla durata del video, tenendo conto della velocità di riproduzione.
// @description:zh-CN      在视频时长旁边显示YouTube视频的剩余时长,考虑播放速度。
// @author                        4lrick
// @match                         https://www.youtube.com/*
// @icon                            https://www.google.com/s2/favicons?sz=64&domain=youtube.com
// @grant                          none
// @license                        GPL-3.0-only
// ==/UserScript==

(function() {
    'use strict';
    let timeDisplay;

    function displayRemainingTime() {
        const videoElement = document.querySelector('video');

        if (videoElement) {
            const timeRemaining = (videoElement.duration - videoElement.currentTime) / videoElement.playbackRate;
            const hours = Math.floor(timeRemaining / 3600);
            const minutes = Math.floor((timeRemaining % 3600) / 60);
            const seconds = Math.floor(timeRemaining % 60);
            const isLive = document.querySelector('.ytp-time-display').classList.contains('ytp-live');

            if (!isLive) {
                timeDisplay.textContent = `(${hours > 0 ? `${hours}:` : ''}${minutes < 10 ? '0' : ''}${minutes}:${seconds < 10 ? '0' : ''}${seconds})`;
            } else {
                timeDisplay.textContent = null;
            }
        }
        requestAnimationFrame(displayRemainingTime);
    }

    function initDisplay() {
        timeDisplay = document.createElement('div');
        timeDisplay.style.display = 'inline-block';
        timeDisplay.style.marginLeft = '10px';
        timeDisplay.style.color = '#ddd';

        const timeContainer = document.querySelector('.ytp-time-display');

        if (timeContainer) {
            timeContainer.appendChild(timeDisplay);
        }

    }

    function checkVideoExists() {
        const videoElement = document.querySelector('video');

        if (videoElement) {
            initDisplay();
            requestAnimationFrame(displayRemainingTime);
            observer.disconnect();
        }
    }

    const observer = new MutationObserver(checkVideoExists);
    const body = document.body;
    const config = { childList: true, subtree: true };
    observer.observe(body, config);
})();