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 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name YouTube | Stop Autoplay (2018 Edition)
  3. // @namespace de.sidneys.userscripts
  4. // @homepage https://gist.githubusercontent.com/sidneys/0a5bea36f989d445cdfbd776023a94ca/raw/
  5. // @version 2.2.0
  6. // @description Disables automatic playback ("Autoplay") of YouTube videos. Works after initial page load and in-page navigation. Compatible with classic & modern YouTube user interface.
  7. // @author sidneys
  8. // @icon https://www.youtube.com/favicon.ico
  9. // @include http*://www.youtube.com/*
  10. // @require https://greasyfork.org/scripts/38888-greasemonkey-color-log/code/Greasemonkey%20%7C%20Color%20Log.js
  11. // @run-at document-start
  12. // ==/UserScript==
  13.  
  14. /**
  15. * ESLint configuration
  16. * @external
  17. */
  18. /* global DEBUG */
  19.  
  20. /**
  21. * @default
  22. * @constant
  23. */
  24. DEBUG = false
  25.  
  26.  
  27. /**
  28. * @default
  29. * @constant
  30. */
  31. const urlPath = '/watch'
  32.  
  33.  
  34. /**
  35. * Stop YouTube Video Player
  36. */
  37. let stopPlayback = () => {
  38. //console.info('stopPlayback')
  39.  
  40. ['play', 'playing', 'timeupdate'].forEach((eventName) => {
  41. const videoElement = document.querySelector('video')
  42.  
  43. /** @listens video:Event */
  44. videoElement.addEventListener(eventName, () => {
  45. console.debug('videoElement', eventName)
  46.  
  47. const playerElement = document.querySelector('.html5-video-player')
  48.  
  49. // Pause Video
  50. playerElement.pauseVideo()
  51.  
  52. // Show Status
  53. console.info('Playback paused.', 'Media Event:', eventName, 'Player State:', playerElement.getPlayerState())
  54. }, { once: true })
  55. })
  56. }
  57.  
  58.  
  59. /**
  60. * Init
  61. */
  62. let init = () => {
  63. console.info('init')
  64.  
  65. // Check URL
  66. if (!window.location.pathname.startsWith(urlPath)) { return }
  67.  
  68. stopPlayback()
  69. }
  70.  
  71.  
  72. /**
  73. * Immediately Invoked Function Expression
  74. */
  75. (() => {
  76. /**
  77. * This event hook covers initial page loading and reloading.
  78. * @listens document:Event#readystatechange
  79. */
  80. document.addEventListener('readystatechange', (event) => {
  81. console.info('document#readystatechange', document.readyState)
  82.  
  83. if (document.readyState === 'interactive') {
  84. init()
  85. }
  86. })
  87.  
  88. /**
  89. * This event hook covers follow-up in-page navigation (classic YouTube user interface).
  90. * @listens window:Event#spfdon
  91. */
  92. window.addEventListener('spfdone', () => {
  93. console.info('window#spfdone')
  94.  
  95. init()
  96. })
  97.  
  98. /**
  99. * This event hook covers in-page navigation within the (modern YouTube user interface).
  100. * @listens window:Event#spfdon
  101. */
  102. window.addEventListener('yt-navigate-finish', () => {
  103. console.info('yt-navigate-finish')
  104.  
  105. init()
  106. })
  107. })()