YouTube Autoplay Stopper For IOS

Prevents YouTube videos from autoplaying and allows manual playback control.

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         YouTube Autoplay Stopper For IOS
// @namespace    assistant-scripts
// @version      1.0
// @description  Prevents YouTube videos from autoplaying and allows manual playback control.
// @author       Assistant
// @match        https://www.youtube.com/*
// @run-at       document-start
// @grant        none
// ==/UserScript==

(function() {
  // Function to pause videos and prevent autoplay
  function stopVideoAutoplay(video) {
    video.pause();
    video.autoplay = false;
    // Ensure video stays paused even if something tries to restart it
    video.addEventListener('play', function pauseOnPlay() {
      video.pause();
      // Remove the listener after first use to allow manual play
      video.removeEventListener('play', pauseOnPlay);
    });
  }

  // MutationObserver to catch video elements as they’re added
  const observer = new MutationObserver((mutations) => {
    const video = document.querySelector('video');
    if (video && !video.dataset.autoplayDisabled) {
      stopVideoAutoplay(video);
      video.dataset.autoplayDisabled = 'true'; // Mark as processed
    }
  });

  // Start observing the document as early as possible
  observer.observe(document.documentElement, { childList: true, subtree: true });

  // Check for existing video elements right away
  window.addEventListener('load', () => {
    const video = document.querySelector('video');
    if (video && !video.dataset.autoplayDisabled) {
      stopVideoAutoplay(video);
      video.dataset.autoplayDisabled = 'true';
    }
  });
})();