prevent-playlist-autoplay

Prevent autoplay for HTML5 video playlists (particularly on YouTube) by making them pause before they can move on. Works on videos in iframes too.

  1. // ==UserScript==
  2. // @name prevent-playlist-autoplay
  3. // @namespace https://github.com/ahuanguchi
  4. // @version 1.1.0
  5. // @description Prevent autoplay for HTML5 video playlists (particularly on YouTube) by making them pause before they can move on. Works on videos in iframes too.
  6. // @author ahuanguchi
  7. // @match *://*/*
  8. // @grant none
  9. // @run-at document-start
  10. // ==/UserScript==
  11.  
  12. (function () {
  13. var video, duration, currentSrc, previousSrc;
  14. var waitTime = 1500;
  15. function checkTime() {
  16. if (duration - video.currentTime < 0.5) {
  17. video.pause();
  18. video.removeEventListener("timeupdate", checkTime);
  19. }
  20. }
  21. function preventPlaylistAutoplay() {
  22. var loc = document.location;
  23. video = document.getElementsByTagName("video")[0];
  24. if (video) {
  25. if (!currentSrc) {
  26. currentSrc = video.src;
  27. }
  28. duration = video.duration;
  29. if (loc.hostname === "www.youtube.com" && loc.search.indexOf("list=") === -1) {
  30. return;
  31. }
  32. video.addEventListener("timeupdate", checkTime);
  33. console.info("Tracking video time.");
  34. } else {
  35. console.info("No video found.");
  36. }
  37. }
  38. function checkSrc() {
  39. if (video) {
  40. previousSrc = currentSrc;
  41. currentSrc = video.src;
  42. if (currentSrc !== previousSrc) {
  43. console.info("Finding next video.");
  44. preventPlaylistAutoplay();
  45. }
  46. }
  47. }
  48. window.addEventListener("load", function () {
  49. window.setTimeout(preventPlaylistAutoplay, waitTime);
  50. });
  51. window.addEventListener("click", function () {
  52. window.setTimeout(checkSrc, waitTime);
  53. });
  54. window.addEventListener("keyup", function () {
  55. window.setTimeout(checkSrc, waitTime);
  56. });
  57. }());