Stop videos looping

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

Bu betiği kurabilmeniz için Tampermonkey, Greasemonkey ya da Violentmonkey gibi bir kullanıcı betiği eklentisini kurmanız gerekmektedir.

You will need to install an extension such as Tampermonkey to install this script.

Bu betiği kurabilmeniz için Tampermonkey ya da Violentmonkey gibi bir kullanıcı betiği eklentisini kurmanız gerekmektedir.

Bu betiği kurabilmeniz için Tampermonkey ya da Userscripts gibi bir kullanıcı betiği eklentisini kurmanız gerekmektedir.

Bu betiği indirebilmeniz için ayrıca Tampermonkey gibi bir eklenti kurmanız gerekmektedir.

Bu komut dosyasını yüklemek için bir kullanıcı komut dosyası yöneticisi uzantısı yüklemeniz gerekecek.

(Zaten bir kullanıcı komut dosyası yöneticim var, kurmama izin verin!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Zateb bir user-style yöneticim var, yükleyeyim!)

// ==UserScript==
// @name         Stop videos looping
// @namespace    http://tampermonkey.net/
// @version      0.72
// @description  Stop videos looping (Youtube, Twitter, Tiktok, Instagram, Facebook)
// @author       @dmtri
// @match        https://*.youtube.com/*
// @match        https://*.x.com/*
// @match        https://*.tiktok.com/*
// @match        https://*.instagram.com/*
// @match        https://*.facebook.com/*
// @license MIT

// @icon
// @grant        none
// ==/UserScript==

(function () {
  "use strict";

  // Keep track of processed videos
  const processedVideos = new WeakSet();

  const init = () => {
    const vids = document.querySelectorAll("video");
    vids.forEach((vid) => {
      if (processedVideos.has(vid)) return;
      processedVideos.add(vid);

      vid.removeAttribute("loop");

      let arr = [];

      const setupTimeout = (extra = 0) => {
        let vidLen = vid.duration;
        let vidCurr = vid.currentTime;
        // ignore any buggy timeout triggered < 500ms
        if ((vidLen - vidCurr) * 1000 < 500) return
        // Ensure duration is available
        if (isNaN(vid.duration) || vid.duration === Infinity) return;

        console.log(vidLen > vidCurr, 'setting a timeout', (vidLen - vidCurr - 0.05) * 1000)
        if (vidLen > vidCurr) {
          const timeout = setTimeout(() => {
           // hack: sometimes video just stops randomly, so need to stop that behavior here
            vidLen = vid.duration;
            vidCurr = vid.currentTime;
            const remaining = (vidLen - vidCurr) * 1000
            console.log(remaining)
            if (remaining < 500) vid.pause();
          }, (vidLen - vidCurr - 0.01) * 1000 + extra); // Adjusted to account for potential delays
          arr.push(timeout);
        }
      };

      // need this for the initial load, 1st time playing. Some extra buffer for 1st time too
      vid.addEventListener('loadedmetadata', () => setupTimeout(-200));

      vid.addEventListener('seeked', () => {
        clearAll(arr);
        setupTimeout();
      });

      vid.addEventListener("pause", () => {
        clearAll(arr);
      });

      vid.addEventListener("play", () => {
        setupTimeout();
      });

      vid.addEventListener("ended", () => {
        setTimeout(() => vid.pause(), 10);
      });
    });
  };

  const clearAll = (arr) => {
    arr.forEach((timeout) => {
      clearTimeout(timeout);
    });
    arr = [];
  };

  const observer = new MutationObserver(init);
  observer.observe(document.body, { childList: true, subtree: true });

  init();
})();