YouTube | Stop Autoplay (2018)

Disabled automatic playback ("Autoplay") of YouTube videos.

目前为 2018-08-19 提交的版本。查看 最新版本

// ==UserScript==
// @name            YouTube | Stop Autoplay (2018)
// @namespace       de.sidneys.userscripts
// @homepage        https://gist.githubusercontent.com/sidneys/0a5bea36f989d445cdfbd776023a94ca/raw/
// @version         1.3.1
// @description     Disabled automatic playback ("Autoplay") of YouTube videos.
// @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-idle
// ==/UserScript==

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

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


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


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

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

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

        // Pause Video
        playerElement.pauseVideo()

        // Show Status
        console.info('Playback paused.', 'getPlayerState()', playerElement.getPlayerState())
    }, {
        once: true,
        passive: true
    })
}


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

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

    stopPlayback()
}


/**
 * Immediately Invoked Function Expression
 */
(() => {
    init()

    /** @listens window:Event#load */
    window.onload = () => {
        console.debug('window#load')

        init()
    }

    /** @listens window:Event#spfdone */
    window.addEventListener('spfdone', () => {
        console.debug('window#spfdone')

        init()
    })
})()