YouTube Autoplay Disable

Script to disable autoplay.

Από την 20/09/2022. Δείτε την τελευταία έκδοση.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey, το Greasemonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

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

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Userscripts για να εγκαταστήσετε αυτόν τον κώδικα.

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

Θα χρειαστεί να εγκαταστήσετε μια επέκταση διαχείρισης κώδικα χρήστη για να εγκαταστήσετε αυτόν τον κώδικα.

(Έχω ήδη έναν διαχειριστή κώδικα χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

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.

(Έχω ήδη έναν διαχειριστή στυλ χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

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

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

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