Greasy Fork

Greasy Fork is available in English.

Kill YouTube Channel Video Autoplay

Kill autoplay on YouTube channel and user pages

当前为 2024-05-12 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name            Kill YouTube Channel Video Autoplay
// @name:de         Kill YouTube Channel Video Autoplay
// @name:en         Kill YouTube Channel Video Autoplay
// @name:es         Kill YouTube Channel Video Autoplay
// @name:it         Kill YouTube Channel Video Autoplay
// @name:ja         Kill YouTube Channel Video Autoplay
// @name:fr         Kill YouTube Channel Video Autoplay
// @name:ko         Kill YouTube Channel Video Autoplay
// @name:pl         Kill YouTube Channel Video Autoplay
// @name:ru         Kill YouTube Channel Video Autoplay
// @name:zh         Kill YouTube Channel Video Autoplay
// @namespace       killYouTubeChannelVideoAutoplay
// @version         0.2.2
// @description     Kill autoplay on YouTube channel and user pages
// @description:de  Beenden Sie die automatische Wiedergabe auf dem YouTube-Kanal und den Benutzerseiten
// @description:en  Kill autoplay on YouTube channel and user pages
// @description:es  Elimina la reproducción automática en el canal de YouTube y en las páginas del usuario
// @description:it  Uccidi la riproduzione automatica sul canale YouTube e sulle pagine utente
// @description:ja  YouTubeチャンネルとユーザーページの自動再生を終了する
// @description:fr  Tuez la lecture automatique sur la chaîne YouTube et les pages utilisateur
// @description:ko  YouTube 채널 및 사용자 페이지에서 자동 재생 중지
// @description:pl  Zabij autoodtwarzanie na kanale YouTube i stronach użytkowników
// @description:ru  Убирает автозапуск видео на страницах канала и пользователя на YouTube
// @description:zh  终止YouTube频道和用户页面上的自动播放
// @author          Blank
// @match           https://www.youtube.com/*
// @run-at          document-end
// @grant           none
// @noframes
// ==/UserScript==

(function main() {
  'use strict';

  const log = (...args) => console.log(`${GM.info.script.name}:`, ...args);
  log('start');

  const root = document.querySelector('ytd-page-manager');
  if (!root) return log('root node not found, exit');

  { // try to prevent autoplay w/o observer
    const video = root.querySelector('ytd-channel-video-player-renderer')?.querySelector('video');
    if (video) {
      video.addEventListener('loadstart', (e) => e.target.pause(), { passive: true });
      log('channel video autoplay prevented w/o observer');
    }
  }

  const observer = new MutationObserver((mutationsList) => {
    const channelRenderer = root.querySelector('ytd-channel-video-player-renderer');
    mutationsList.some((mutationRecord) => {
      if (!mutationRecord.target.classList.contains('html5-video-container')) return false;
      return Array.from(mutationRecord.addedNodes).some((node) => {
        if (node.nodeName === 'VIDEO') {
          log('video captured');
          if (channelRenderer?.contains(node)) {
            // observer.disconnect();
            node.addEventListener('loadstart', (e) => e.target.pause(), { passive: true });
            log('channel video autoplay prevented');
          }
        }
      });
    });
  });
  observer.observe(root, { childList: true, subtree: true });
  return log('observer observe');
}());