Loop Fix

Fixes youtube video repeating when watching in playlist + refreshing page if video stopped downloading

当前为 2024-09-30 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Loop Fix
  3. // @description Fixes youtube video repeating when watching in playlist + refreshing page if video stopped downloading
  4. // @version 0.1.18
  5. // @author 0vC4
  6. // @namespace https://greasyfork.org/users/670183
  7. // @match *://*.youtube.com/watch*
  8. // @match *://*.youtube.com/shorts*
  9. // @icon https://www.google.com/s2/favicons?sz=64&domain=youtube.com
  10. // @run-at document-start
  11. // @license MIT
  12. // @grant none
  13. // ==/UserScript==
  14.  
  15. // refresh if no reply during these actions
  16. const maxLoadingTime = 500;
  17. const maxSeekingTime = 1500;
  18.  
  19. (() => {
  20. function blockEvents(condition, ...events) {
  21. events = events.flat();
  22. const tag = window.EventTarget.prototype;
  23. tag._add = tag.addEventListener;
  24. tag.addEventListener = function (name, callback, options) {
  25. if (!events.includes(name)) return tag._add.call(this, name, callback, options);
  26.  
  27. function cb(e) {
  28. if (!condition.call(this)) return;
  29. callback.call(this, e);
  30. };
  31.  
  32. tag._add.call(this, name, cb, options);
  33. };
  34. }
  35.  
  36. blockEvents(function() {
  37. return !this.loop; // have loop = block events
  38. }, 'pause', 'timeupdate', 'waiting');
  39. })();
  40.  
  41. const log = window.console.log;
  42. window.loopState = window.localStorage.getItem('loopState') ?? false;
  43. function clicking(e) {
  44. window.loopState = JSON.parse(this.ariaChecked);
  45. };
  46.  
  47. let created = false;
  48. let refreshing = false;
  49.  
  50. let loaded = false;
  51. let navigating = true;
  52.  
  53. let seeking = false;
  54. let seekingId = 0;
  55.  
  56. const policy = window.trustedTypes && window.trustedTypes.createPolicy ? window.trustedTypes.createPolicy('timeout', {createScriptURL: str => str}) : {createScriptURL: str => str};
  57. const timeout = delay => URL.createObjectURL(new Blob([`setTimeout(() => postMessage(0), ${delay});`]));
  58. const refreshOnLoad = policy.createScriptURL(timeout(maxLoadingTime));
  59. const seekingDebounce = policy.createScriptURL(timeout(500));
  60.  
  61. document.addEventListener('yt-navigate-start', () => {
  62. navigating = true;
  63. loaded = false;
  64. });
  65. document.addEventListener('yt-navigate-finish', () => {
  66. navigating = false;
  67. });
  68.  
  69. let focused = false;
  70. window.setInterval(()=>{
  71. if (!focused) {
  72. focused = document.hasFocus();
  73. return;
  74. }
  75.  
  76. const vid = window.document.querySelector('video.html5-main-video');
  77. if (vid) {
  78. if (!created) {
  79. created = true;
  80. window.localStorage.removeItem('loopState');
  81. vid.addEventListener('seeking', () => {
  82. clearTimeout(seekingId);
  83. seeking = true;
  84. seekingId = setTimeout(() => {
  85. seeking = false;
  86. }, maxSeekingTime);
  87. });
  88.  
  89. // refresh if no data for half sec
  90. const callback = () => {
  91. if (!loaded && vid.readyState === window.HTMLMediaElement.HAVE_NOTHING) {
  92. window.localStorage.setItem('loopState', window.loopState);
  93. window.location.href = window.location.href;
  94. }
  95. };
  96. new Worker(refreshOnLoad).onmessage = callback;
  97. }
  98.  
  99. // to prevent early page refresh
  100. if (vid.readyState === window.HTMLMediaElement.HAVE_CURRENT_DATA || vid.readyState === window.HTMLMediaElement.HAVE_ENOUGH_DATA) loaded = !navigating;
  101.  
  102. const noData = vid.readyState === window.HTMLMediaElement.HAVE_CURRENT_DATA || vid.readyState === window.HTMLMediaElement.HAVE_METADATA;
  103.  
  104. if (loaded && !refreshing && noData && !seeking && !vid.paused && +vid.currentTime.toFixed(0) >= +vid.buffered.end(0).toFixed(0) - 2 && +vid.currentTime.toFixed(0) < +vid.duration.toFixed(0) - 2) {
  105. refreshing = true;
  106. const callback = () => {
  107. if (seeking) {
  108. refreshing = false;
  109. return;
  110. }
  111. window.localStorage.setItem('loopState', window.loopState);
  112. window.location.href = window.location.href.split('?')[0] + '?t=' + (+vid.currentTime.toFixed(0) + 1) + '&' + window.location.href.split('?')[1];
  113. };
  114. new Worker(seekingDebounce).onmessage = callback;
  115. }
  116.  
  117. // apply loopState after .src or page refresh
  118. if (vid.loop != window.loopState) vid.loop = window.loopState;
  119.  
  120. // attach loop click detector
  121. const repeat = window.document.querySelectorAll('.ytp-menuitem[tabindex="-1"]')[0];
  122. if (repeat && repeat.onclick != clicking) {
  123. repeat.onclick = clicking;
  124. }
  125. }
  126. });