YouTube Shorts Auto Next

Automatically plays the next YouTube short.

  1. // ==UserScript==
  2. // @name YouTube Shorts Auto Next
  3. // @description Automatically plays the next YouTube short.
  4. // @version 2024-03-02
  5. // @author Santeri Hetekivi
  6. // @match https://www.youtube.com/shorts/*
  7. // @icon https://www.google.com/s2/favicons?sz=64&domain=youtube.com
  8. // @grant none
  9. // @license Apache-2.0
  10. // @namespace https://gf.qytechs.cn/users/164021
  11. // ==/UserScript==
  12.  
  13. (function () {
  14. 'use strict';
  15.  
  16. /**
  17. * Get video element.
  18. * @returns {HTMLVideoElement} The video element.
  19. */
  20. function getVideo() {
  21. // Get all video elements with src attribute.
  22. const videos = document.querySelectorAll("video[src]")
  23.  
  24. // Get elements length.
  25. const videosLength = videos.length
  26. // If there is only one video element
  27. if (videosLength === 1) {
  28. // init get video timeout counter.
  29. getVideo.timeoutCounter = 0
  30. const video = videos[0]
  31. if (!(video instanceof HTMLVideoElement)) {
  32. throw new Error("Video is not a HTMLVideoElement!")
  33. }
  34. return video
  35. }
  36. // Else if no video elements found
  37. else if (videosLength === 0) {
  38. // Set timeout for getting video.
  39. getVideo.timeout = setTimeout(
  40. function () {
  41. // Clear timeout.
  42. clearTimeout(getVideo.timeout)
  43. getVideo.timeout = null
  44. return getVideo()
  45. },
  46. // Timeout time is 1000ms * times this has been called..
  47. 1000 * (++getVideo.timeoutCounter)
  48. );
  49. }
  50. // Else if there are more than one video elements, throw error.
  51. else {
  52. throw new Error("Found " + videosLength + " video elements with src attribute!")
  53. }
  54. }
  55.  
  56. // Get and initialise video element.
  57. const video = getVideo()
  58.  
  59. /**
  60. * Init video element.
  61. */
  62. function initVideo() {
  63. // Set autoplay to false.
  64. video.autoplay = false
  65. // Set loop to false.
  66. video.loop = false
  67. }
  68.  
  69. // Init video.
  70. initVideo()
  71.  
  72. // Add event listener for ended event.
  73. video.addEventListener(
  74. "ended",
  75. function () {
  76. // Get next button.
  77. const button = document.querySelector("#navigation-button-down button")
  78. if (!(button instanceof HTMLButtonElement)) {
  79. throw new Error("No next button found!")
  80. }
  81. // Click the next button.
  82. button.click()
  83. }
  84. )
  85.  
  86. // Add event listener for progress event.
  87. video.addEventListener(
  88. "progress",
  89. function () {
  90. // Init video again, because Youtube resets video element.
  91. initVideo()
  92. }
  93. )
  94. })();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址