YouTube | Stop Autoplay (2018 Edition)

Disables automatic playback ("Autoplay") of YouTube videos. Works after initial page load and in-page navigation. Compatible with classic & modern YouTube user interface.

目前為 2018-08-20 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name            YouTube | Stop Autoplay (2018 Edition)
// @namespace       de.sidneys.userscripts
// @homepage        https://gist.githubusercontent.com/sidneys/0a5bea36f989d445cdfbd776023a94ca/raw/
// @version         2.2.0
// @description     Disables automatic playback ("Autoplay") of YouTube videos. Works after initial page load and in-page navigation. Compatible with classic & modern YouTube user interface.
// @author          sidneys
// @icon            https://www.youtube.com/favicon.ico
// @include         http*://www.youtube.com/*
// @require         https://greasyfork.org/scripts/38888-greasemonkey-color-log/code/Greasemonkey%20%7C%20Color%20Log.js
// @run-at          document-start
// ==/UserScript==

/**
 * ESLint configuration
 * @external
 */
/* global DEBUG */

/**
 * @default
 * @constant
 */
DEBUG = false


/**
 * @default
 * @constant
 */
const urlPath = '/watch'


/**
 * Stop YouTube Video Player
 */
let stopPlayback = () => {
    //console.info('stopPlayback')

    ['play', 'playing', 'timeupdate'].forEach((eventName) => {
        const videoElement = document.querySelector('video')

        /** @listens video:Event */
        videoElement.addEventListener(eventName, () => {
            console.debug('videoElement', eventName)

            const playerElement = document.querySelector('.html5-video-player')

            // Pause Video
            playerElement.pauseVideo()

            // Show Status
            console.info('Playback paused.', 'Media Event:', eventName, 'Player State:', playerElement.getPlayerState())
        }, { once: true })
    })
}


/**
 * Init
 */
let init = () => {
    console.info('init')

    // Check URL
    if (!window.location.pathname.startsWith(urlPath)) { return }

    stopPlayback()
}


/**
 * Immediately Invoked Function Expression
 */
(() => {
    /**
     * This event hook covers initial page loading and reloading.
     * @listens document:Event#readystatechange
     */
    document.addEventListener('readystatechange', (event) => {
        console.info('document#readystatechange', document.readyState)

        if (document.readyState === 'interactive') {
            init()
        }
    })

    /**
     * This event hook covers follow-up in-page navigation (classic YouTube user interface).
     * @listens window:Event#spfdon
     */
    window.addEventListener('spfdone', () => {
        console.info('window#spfdone')

        init()
    })

    /**
     * This event hook covers in-page navigation within the (modern YouTube user interface).
     * @listens window:Event#spfdon
     */
    window.addEventListener('yt-navigate-finish', () => {
        console.info('yt-navigate-finish')

        init()
    })
})()