YouTube | Stop Video Autoplay

Stop automatic video playback everywhere. Works on first page load & after navigating.

目前为 2018-11-30 提交的版本。查看 最新版本

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