YouTube Autoplay Disable

Script to disable autoplay.

当前为 2022-07-28 提交的版本,查看 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name            YouTube Autoplay Disable
// @name:ja         YouTube 自動再生 無効化
// @namespace       https://midra.me
// @version         0.0.1
// @description     Script to disable autoplay.
// @description:ja  自動再生を無効化するスクリプト。
// @author          Midra
// @license         MIT
// @match           https://www.youtube.com/*
// @icon            https://www.google.com/s2/favicons?sz=64&domain=youtube.com
// @run-at          document-start
// @grant           GM_getValue
// @grant           GM_setValue
// @grant           GM_registerMenuCommand
// @require         https://greasyfork.org/scripts/7212-gm-config-eight-s-version/code/GM_config%20(eight's%20version).js?version=156587
// ==/UserScript==

(() => {
  const language = (window.navigator.languages && window.navigator.languages[0]) || window.navigator.language || window.navigator.userLanguage || window.navigator.browserLanguage
  const lang = language === 'ja-JP' ? 'ja' : 'en'

  const i18n = {
    configTitle: {
      ja: '自動再生を無効化する対象',
      en: 'Target to disable autoplay',
    },
    configSaveAlert: {
      ja: '設定の変更を反映させるにはページを再読み込みしてください。',
      en: 'Please reload the page for the configuration changes to take effect.',
    },
    configItems: {
      default: {
        ja: '通常の動画',
        en: 'Normal video',
      },
      live: {
        ja: 'ライブ',
        en: 'Live',
      },
      channel: {
        ja: 'チャンネルのホーム',
        en: 'Channel home',
      },
      iframe: {
        ja: '埋め込み動画',
        en: 'Embedded video',
      },
    },
  }

  const configInitData = {
    default: {
      label: i18n.configItems.default[lang],
      type: 'checkbox',
      default: true,
    },
    live: {
      label: i18n.configItems.live[lang],
      type: 'checkbox',
      default: true,
    },
    channel: {
      label: i18n.configItems.channel[lang],
      type: 'checkbox',
      default: true,
    },
    iframe: {
      label: i18n.configItems.iframe[lang],
      type: 'checkbox',
      default: true,
    },
  }

  GM_config.init(i18n.configTitle[lang], configInitData)

  GM_config.onload = () => {
    setTimeout(() => {
      alert(i18n.configSaveAlert[lang])
    }, 200)
  }

  GM_registerMenuCommand('設定', GM_config.open)

  // 設定取得
  const config = {}
  Object.keys(configInitData).forEach(v => { config[v] = GM_config.get(v) })

  if (window === window.parent) {
    window.addEventListener('yt-player-updated', ({ target, detail }) => {
      if (!(target instanceof HTMLElement)) return

      const videoData = detail.getVideoData()

      if (
        // 通常の動画
        (
          config['default'] &&
          target.id === 'ytd-player' &&
          !videoData.isLive
        ) ||
        // ライブ
        (
          config['live'] &&
          target.id === 'ytd-player' &&
          videoData.isLive
        ) ||
        // チャンネルのホーム
        (
          config['channel'] &&
          target.classList.contains('ytd-channel-video-player-renderer')
        )
      ) {
        target.stop()
      }
    })
  }
  else if (
    config['iframe'] &&
    window.location.href.startsWith('https://www.youtube.com/embed/')
  ) {
    const url = new URL(window.location.href)
    if (
      url.searchParams.get('autoplay') !== '0' ||
      url.searchParams.get('auto_play') !== 'false'
    ) {
      url.searchParams.set('autoplay', '0')
      url.searchParams.set('auto_play', 'false')
      window.location.href = url.href
    }
  }
})()