Stop videos looping

Stop videos looping (Youtube, Twitter, Tiktok, Instagram, Facebook)

התקן את הסקריפט?
סקריפטים מומלצים של יוצר זה

אולי תאהב גם את YouTube Channel Hover Popup.

התקן את הסקריפט
  1. // ==UserScript==
  2. // @name Stop videos looping
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.72
  5. // @description Stop videos looping (Youtube, Twitter, Tiktok, Instagram, Facebook)
  6. // @author @dmtri
  7. // @match https://*.youtube.com/*
  8. // @match https://*.x.com/*
  9. // @match https://*.tiktok.com/*
  10. // @match https://*.instagram.com/*
  11. // @match https://*.facebook.com/*
  12. // @license MIT
  13.  
  14. // @icon
  15. // @grant none
  16. // ==/UserScript==
  17.  
  18. (function () {
  19. "use strict";
  20.  
  21. // Keep track of processed videos
  22. const processedVideos = new WeakSet();
  23.  
  24. const init = () => {
  25. const vids = document.querySelectorAll("video");
  26. vids.forEach((vid) => {
  27. if (processedVideos.has(vid)) return;
  28. processedVideos.add(vid);
  29.  
  30. vid.removeAttribute("loop");
  31.  
  32. let arr = [];
  33.  
  34. const setupTimeout = (extra = 0) => {
  35. let vidLen = vid.duration;
  36. let vidCurr = vid.currentTime;
  37. // ignore any buggy timeout triggered < 500ms
  38. if ((vidLen - vidCurr) * 1000 < 500) return
  39. // Ensure duration is available
  40. if (isNaN(vid.duration) || vid.duration === Infinity) return;
  41.  
  42. console.log(vidLen > vidCurr, 'setting a timeout', (vidLen - vidCurr - 0.05) * 1000)
  43. if (vidLen > vidCurr) {
  44. const timeout = setTimeout(() => {
  45. // hack: sometimes video just stops randomly, so need to stop that behavior here
  46. vidLen = vid.duration;
  47. vidCurr = vid.currentTime;
  48. const remaining = (vidLen - vidCurr) * 1000
  49. console.log(remaining)
  50. if (remaining < 500) vid.pause();
  51. }, (vidLen - vidCurr - 0.01) * 1000 + extra); // Adjusted to account for potential delays
  52. arr.push(timeout);
  53. }
  54. };
  55.  
  56. // need this for the initial load, 1st time playing. Some extra buffer for 1st time too
  57. vid.addEventListener('loadedmetadata', () => setupTimeout(-200));
  58.  
  59. vid.addEventListener('seeked', () => {
  60. clearAll(arr);
  61. setupTimeout();
  62. });
  63.  
  64. vid.addEventListener("pause", () => {
  65. clearAll(arr);
  66. });
  67.  
  68. vid.addEventListener("play", () => {
  69. setupTimeout();
  70. });
  71.  
  72. vid.addEventListener("ended", () => {
  73. setTimeout(() => vid.pause(), 10);
  74. });
  75. });
  76. };
  77.  
  78. const clearAll = (arr) => {
  79. arr.forEach((timeout) => {
  80. clearTimeout(timeout);
  81. });
  82. arr = [];
  83. };
  84.  
  85. const observer = new MutationObserver(init);
  86. observer.observe(document.body, { childList: true, subtree: true });
  87.  
  88. init();
  89. })();