YouTube Autoplay Disable

Script to disable autoplay.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name            YouTube Autoplay Disable
// @name:ja         YouTube 自動再生 無効化
// @namespace       https://midra.me
// @version         1.0.2
// @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==

(() => {
  console.log('[YouTube Autoplay Disable]: v1.0.2')

  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') === '1') {
      url.searchParams.set('autoplay', '0')
      window.history.replaceState(null, '', url.href)
      window.location.reload()
    }
  }
})()