Loop Fix

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

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

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