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.6
  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 isEmbed = window.location.href.includes("youtube.com/embed");
  31. const timeDisplayElement = document.querySelector('.ytp-time-display');
  32.  
  33. if ((timeDisplayElement && !timeDisplayElement.classList.contains('ytp-live')) && !isEmbed) {
  34. timeDisplay.textContent = `(${hours > 0 ? `${hours}:` : ''}${minutes < 10 ? '0' : ''}${minutes}:${seconds < 10 ? '0' : ''}${seconds})`;
  35. } else {
  36. timeDisplay.textContent = null;
  37. }
  38. }
  39. requestAnimationFrame(displayRemainingTime);
  40. }
  41.  
  42. function initDisplay() {
  43. timeDisplay = document.createElement('div');
  44. timeDisplay.style.display = 'inline-block';
  45. timeDisplay.style.marginLeft = '10px';
  46. timeDisplay.style.color = '#ddd';
  47.  
  48. const timeContainer = document.querySelector('.ytp-time-display');
  49.  
  50. if (timeContainer) {
  51. timeContainer.appendChild(timeDisplay);
  52. }
  53.  
  54. }
  55.  
  56. function checkVideoExists() {
  57. const videoElement = document.querySelector('video');
  58.  
  59. if (videoElement) {
  60. initDisplay();
  61. requestAnimationFrame(displayRemainingTime);
  62. observer.disconnect();
  63. }
  64. }
  65.  
  66. const observer = new MutationObserver(checkVideoExists);
  67. const body = document.body;
  68. const config = { childList: true, subtree: true };
  69. observer.observe(body, config);
  70. })();