YouTube - Remaining time

在视频时长旁边显示YouTube视频的剩余时长,考虑播放速度。

当前为 2024-03-20 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name YouTube - Remaining time
  3. // @namespace https://gist.github.com/4lrick/cf14cf267684f06c1b7bc559ddf2b943
  4. // @version 1.7
  5. // @description Displays the remaining duration of a YouTube video next to the video duration, taking into account the playback rate.
  6. // @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.
  7. // @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.
  8. // @description:de Zeigt die verbleibende Dauer eines YouTube-Videos neben der Videodauer an und berücksichtigt dabei die Wiedergabegeschwindigkeit.
  9. // @description:it Mostra la durata rimanente di un video di YouTube accanto alla durata del video, tenendo conto della velocità di riproduzione.
  10. // @description:zh-CN 在视频时长旁边显示YouTube视频的剩余时长,考虑播放速度。
  11. // @author 4lrick
  12. // @match https://www.youtube.com/*
  13. // @icon https://www.google.com/s2/favicons?sz=64&domain=youtube.com
  14. // @grant none
  15. // @license GPL-3.0-only
  16. // ==/UserScript==
  17.  
  18. (function() {
  19. 'use strict';
  20. let timeDisplay;
  21.  
  22. function displayRemainingTime() {
  23. const videoElement = document.querySelector('video');
  24.  
  25. if (videoElement) {
  26. const timeRemaining = (videoElement.duration - videoElement.currentTime) / videoElement.playbackRate;
  27. const hours = Math.floor(timeRemaining / 3600);
  28. const minutes = Math.floor((timeRemaining % 3600) / 60);
  29. const seconds = Math.floor(timeRemaining % 60);
  30. const isLive = document.querySelector('.ytp-time-display').classList.contains('ytp-live');
  31.  
  32. if (!isLive) {
  33. timeDisplay.textContent = `(${hours > 0 ? `${hours}:` : ''}${minutes < 10 ? '0' : ''}${minutes}:${seconds < 10 ? '0' : ''}${seconds})`;
  34. } else {
  35. timeDisplay.textContent = null;
  36. }
  37. }
  38. requestAnimationFrame(displayRemainingTime);
  39. }
  40.  
  41. function initDisplay() {
  42. timeDisplay = document.createElement('div');
  43. timeDisplay.style.display = 'inline-block';
  44. timeDisplay.style.marginLeft = '10px';
  45. timeDisplay.style.color = '#ddd';
  46.  
  47. const timeContainer = document.querySelector('.ytp-time-display');
  48.  
  49. if (timeContainer) {
  50. timeContainer.appendChild(timeDisplay);
  51. }
  52.  
  53. }
  54.  
  55. function checkVideoExists() {
  56. const videoElement = document.querySelector('video');
  57.  
  58. if (videoElement) {
  59. initDisplay();
  60. requestAnimationFrame(displayRemainingTime);
  61. observer.disconnect();
  62. }
  63. }
  64.  
  65. const observer = new MutationObserver(checkVideoExists);
  66. const body = document.body;
  67. const config = { childList: true, subtree: true };
  68. observer.observe(body, config);
  69. })();