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.6
  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. (() => {
  15. function blockEvents(condition, ...events) {
  16. events = events.flat();
  17. const tag = window.EventTarget.prototype;
  18. tag._add = tag.addEventListener;
  19. tag.addEventListener = function (name, callback, options) {
  20. if (!events.includes(name)) return tag._add.call(this, name, callback, options);
  21.  
  22. function cb(e) {
  23. if (!condition.call(this)) return;
  24. callback.call(this, e);
  25. };
  26.  
  27. tag._add.call(this, name, cb, options);
  28. };
  29. }
  30.  
  31. blockEvents(function() {
  32. return !this.loop; // have loop = block events
  33. }, 'pause', 'timeupdate', 'waiting');
  34. })();
  35.  
  36. const log = window.console.log;
  37. window.loopState = localStorage.getItem('loopState') ?? false;
  38. function clicking(e) {
  39. window.loopState = JSON.parse(this.ariaChecked);
  40. };
  41.  
  42. let created = false;
  43. let refreshing = false;
  44. let loaded = false;
  45. let oldVid = null;
  46. window.setInterval(()=>{
  47. const vid = window.document.querySelector('video.html5-main-video');
  48. if (vid) {
  49. // to prevent false detecting when changing video
  50. if (vid != oldVid) {
  51. loaded = false;
  52. oldVid = vid;
  53. }
  54.  
  55. if (!created) {
  56. created = true;
  57. localStorage.removeItem('loopState');
  58.  
  59. // refresh if no data for half sec
  60. const callback = () => {
  61. if (!loaded && vid.readyState === window.HTMLMediaElement.HAVE_NOTHING) {
  62. localStorage.setItem('loopState', window.loopState);
  63. window.location.href = window.location.href;
  64. }
  65. };
  66. const blob = new window.Blob([`setTimeout(() => postMessage(0), 500);`]);
  67. const workerScript = window.URL.createObjectURL(blob);
  68. const script = window.trustedTypes && window.trustedTypes.createPolicy ? window.trustedTypes.createPolicy('timeout', {createScriptURL: str => str}).createScriptURL(workerScript) : workerScript;
  69. const worker = new window.Worker(script);
  70. worker.onmessage = callback;
  71. }
  72.  
  73. // apply loopState after .src or page refresh
  74. if (vid.loop != window.loopState) vid.loop = window.loopState;
  75.  
  76. // to prevent early page refresh
  77. if (vid.readyState === window.HTMLMediaElement.HAVE_CURRENT_DATA) loaded = true;
  78.  
  79. const noData = vid.readyState === window.HTMLMediaElement.HAVE_CURRENT_DATA || vid.readyState === window.HTMLMediaElement.HAVE_METADATA;
  80. if (loaded && !refreshing && noData && !vid.paused && +vid.currentTime.toFixed(0) >= +vid.buffered.end(0).toFixed(0) - 1) {
  81. localStorage.setItem('loopState', window.loopState);
  82. window.location.href = window.location.href.split('?')[0] + '?t=' + vid.currentTime.toFixed(0) + '&' + window.location.href.split('?')[1];
  83. refreshing = true;
  84. }
  85. }
  86.  
  87. // attach loop click detector
  88. const repeat = window.document.querySelectorAll('.ytp-menuitem[tabindex="-1"]')[0];
  89. if (repeat && repeat.onclick != clicking) {
  90. repeat.onclick = clicking
  91. }
  92. });