Add YouTube Video Progress

Add progress bar at bottom of YouTube video and progress text on the video page.

当前为 2018-02-06 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Add YouTube Video Progress
  3. // @namespace https://gf.qytechs.cn/en/users/85671-jcunews
  4. // @version 1.0.6
  5. // @license GNU AGPLv3
  6. // @description Add progress bar at bottom of YouTube video and progress text on the video page.
  7. // @author jcunews
  8. // @match https://www.youtube.com/*
  9. // @grant none
  10. // @run-at document-start
  11. // ==/UserScript==
  12.  
  13. //===== Configuration Start =====
  14. progressbarHeight = 3; //in pixels
  15. progressbarColor = "#fff"; //e.g. "#fff" or "#e0e0e0" or "cyan"
  16. progressbarElapsedColor = "#f00";
  17. //===== Configuration End =====
  18.  
  19. addEventListener("yt-page-data-updated", function() {
  20. if (window.vidprogress || (location.pathname !== "/watch")) return;
  21. (function waitInfo(a, b) {
  22. if (a = document.querySelector("#info-contents #info")) {
  23. b = document.createElement("SPAN");
  24. b.id = "vidprogress";
  25. b.innerHTML = '<span id="curq" style="font-weight:500"></span><span id="curtime" style="margin-left:2ex"></span>';
  26. b.style.cssText = "border:1px solid #ccc;border-radius:4px;padding:2px 5px;background:#eee;";
  27. if (window["body-container"]) {
  28. b.style.cssText += "margin-left:2ex";
  29. a.appendChild(b);
  30. } else {
  31. b.style.cssText += "margin-left:2ex;font-size:10pt";
  32. a.insertBefore(b, a.querySelector("#flex"));
  33. }
  34. } else setTimeout(waitInfo, 200);
  35. })();
  36. });
  37.  
  38. addEventListener("yt-player-released", function() {
  39. var timerProgressMonitor, timerDoubleCheck;
  40.  
  41. function zerolead(n){
  42. return n > 9 ? n : "0" + n;
  43. }
  44.  
  45. function sec2hms(sec) {
  46. var c = sec % 60, d = Math.floor(sec / 60);
  47. return (d >= 60 ? zerolead(Math.floor(d / 60)) + ":" : "") + zerolead(d % 60) + ":" + zerolead(c);
  48. }
  49.  
  50. function updProgress(a, b, c, ls){
  51. a = window.movie_player;
  52. if (a && a.getCurrentTime) try {
  53. if (window.curtime) {
  54. b = a.getPlaybackQuality();
  55. switch (b) {
  56. case "light":
  57. case "tiny": c = "144p"; break;
  58. case "small": c = "240p"; break;
  59. case "medium": c = "360p"; break;
  60. case "large": c = "480p"; break;
  61. case "highres": c = "4320p"; break;
  62. default:
  63. if (ls = b.match(/^hd(\d+)/)) {
  64. c = ls[1] + "p";
  65. } else c = b;
  66. }
  67. curq.textContent = c;
  68. curq.title = b;
  69. }
  70. b = a.getCurrentTime();
  71. if (b >= 0) {
  72. ls = a.getDuration();
  73. if (!a.getVideoData().isLive) {
  74. if (window.curtime) {
  75. curtime.textContent = sec2hms(Math.floor(b)) + " / " + sec2hms(Math.floor(ls)) + " (" + Math.floor(b * 100 / ls) + "%)";
  76. }
  77. vidprogress2b.style.width = Math.ceil((b / ls) * vidprogress2.offsetWidth) + "px";
  78. } else {
  79. if (window.curtime) {
  80. curtime.textContent = "LIVE";
  81. }
  82. vidprogress2b.style.width = "100%";
  83. }
  84. } else {
  85. if (window.curtime) {
  86. curq.textContent = "";
  87. curtime.textContent = "";
  88. }
  89. vidprogress2b.style.width = "0px";
  90. }
  91. }catch(a){
  92. if (window.curtime) {
  93. curq.textContent = "[???]";
  94. curtime.textContent = "???";
  95. }
  96. vidprogress2b.style.width = "0px";
  97. }
  98. }
  99.  
  100. function resumeProgressMonitor() {
  101. if (timerProgressMonitor) return;
  102. updProgress();
  103. timerProgressMonitor = setInterval(updProgress, 200);
  104. }
  105.  
  106. function pauseProgressMonitor() {
  107. clearInterval(timerProgressMonitor);
  108. timerProgressMonitor = 0;
  109. updProgress();
  110. }
  111.  
  112. (function waitPlayer() {
  113. if (!window.vidprogress2 && window.movie_player && (a = movie_player.parentNode.querySelector("video"))) {
  114. b = document.createElement("DIV");
  115. b.id = "vidprogress2";
  116. b.style.cssText = "opacity:.66;position:absolute;z-index:10;bottom:0;width:100%;height:" + progressbarHeight + "px;background:" + progressbarColor;
  117. b.innerHTML = '<div id="vidprogress2b" style="height:100%;background:' + progressbarElapsedColor + '"></div>';
  118. movie_player.appendChild(b);
  119. if (movie_player.getPlayerState() === 1) {
  120. resumeProgressMonitor();
  121. }
  122. //useful: onLoadedMetadata(), onStateChange(state), onPlayVideo(info), onReady(playerApi), onVideoAreaChange(), onVideoDataChange(info)
  123. //states: -1=notReady, 0=ended, 1=playing, 2=paused, 3=ready, 4=???, 5=notAvailable?
  124. movie_player.addEventListener("onLoadedMetadata", resumeProgressMonitor);
  125. movie_player.addEventListener("onStateChange", function(state) {
  126. if (state === 1) {
  127. resumeProgressMonitor();
  128. } else pauseProgressMonitor();
  129. });
  130. } else setTimeout(waitPlayer, 200);
  131. })();
  132.  
  133. function doubleCheck() {
  134. if (window.movie_player && movie_player.getPlayerState) {
  135. if (movie_player.getPlayerState() === 1) {
  136. resumeProgressMonitor();
  137. } else pauseProgressMonitor();
  138. }
  139. }
  140. if (!timerDoubleCheck) timerDoubleCheck = setInterval(doubleCheck, 500);
  141. });

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址