Loop Fix

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

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

  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.3
  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 lock = false;
  43. let was = false;
  44. let was_vid1 = false;
  45. window.setInterval(()=>{
  46. const vid1 = window.document.querySelector('video.html5-main-video');
  47. if (vid1 && !was_vid1) {
  48. was_vid1 = true;
  49. localStorage.removeItem('loopState');
  50. }
  51. if (vid1 && was && !lock && (vid1.readyState === window.HTMLMediaElement.HAVE_CURRENT_DATA || vid1.readyState === window.HTMLMediaElement.HAVE_METADATA) && !vid1.paused && +vid1.currentTime.toFixed(0) >= +vid1.buffered.end(0).toFixed(0)) {
  52. localStorage.setItem('loopState', window.loopState);
  53. window.location.href = window.location.href.split('?')[0] + '?t=' + vid1.currentTime.toFixed(0) + '&' + window.location.href.split('?')[1];
  54. lock = true;
  55. }
  56. if (vid1 && vid1.readyState === window.HTMLMediaElement.HAVE_CURRENT_DATA) was = true;
  57.  
  58. const repeat = window.document.querySelectorAll('.ytp-menuitem[tabindex="-1"]')[0];
  59. if (repeat && repeat.onclick != clicking) {
  60. repeat.onclick = clicking
  61. }
  62. const vid = window.document.querySelector('video.html5-main-video');
  63. if (vid && vid.loop != window.loopState) {
  64. vid.loop = window.loopState;
  65. }
  66. });