YouTube | Stop Autoplay (2018)

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

当前为 2018-08-19 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name YouTube | Stop Autoplay (2018)
  3. // @namespace de.sidneys.userscripts
  4. // @homepage https://gist.githubusercontent.com/sidneys/0a5bea36f989d445cdfbd776023a94ca/raw/
  5. // @version 1.5.0
  6. // @description Disabled automatic playback ("Autoplay") of YouTube videos.
  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-idle
  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'].forEach((eventName) => {
  41. const videoElement = document.querySelector('video')
  42.  
  43. /** @listens video:Event#play */
  44. /** @listens video:Event#playing */
  45. videoElement.addEventListener(eventName, () => {
  46. console.debug('videoElement', eventName)
  47.  
  48. const playerElement = document.querySelector('.html5-video-player')
  49.  
  50. // Pause Video
  51. playerElement.pauseVideo()
  52.  
  53. // Seek Video
  54. playerElement.seekTo(3)
  55.  
  56. // Show Status
  57. console.info('Playback paused.', 'getPlayerState()', playerElement.getPlayerState())
  58. }, { once: true })
  59. })
  60. }
  61.  
  62.  
  63. /**
  64. * Init
  65. */
  66. let init = () => {
  67. console.debug('init')
  68.  
  69. // Check URL
  70. if (!window.location.pathname.startsWith(urlPath)) { return }
  71.  
  72. stopPlayback()
  73. }
  74.  
  75.  
  76. /**
  77. * Immediately Invoked Function Expression
  78. */
  79. (() => {
  80. /** @listens window:Event#load */
  81. window.onload = () => {
  82. console.debug('window#load')
  83.  
  84. init()
  85. }
  86.  
  87. /** @listens window:Event#spfdone */
  88. window.addEventListener('spfdone', () => {
  89. console.debug('window#spfdone')
  90.  
  91. init()
  92. })
  93. })()