YouTube chapter auto open

Automatically opens the YouTube chapter selection.

  1. // ==UserScript==
  2. // @name YouTube chapter auto open
  3. // @description Automatically opens the YouTube chapter selection.
  4. // @namespace Violentmonkey Scripts
  5. // @match https://www.youtube.com/*
  6. // @match https://m.youtube.com/*
  7. // @grant none
  8. // @version 2.0.1
  9. // @author AstragoDE (https://github.com/AstragoDE)
  10. // @run-at document-end
  11. // @homepageURL https://github.com/AstragoTech/youtube_chapter_auto_open
  12. // @icon https://www.google.com/s2/favicons?domain=youtube.com
  13. // @compatible chrome Chrome + Tampermonkey or Violentmonkey
  14. // @compatible firefox Firefox + Greasemonkey or Tampermonkey or Violentmonkey
  15. // @compatible opera Opera + Tampermonkey or Violentmonkey
  16. // @compatible edge Edge + Tampermonkey or Violentmonkey
  17. // @compatible safari Safari + Tampermonkey or Violentmonkey
  18. // @license This work © 2022 by AstragoDE is licensed under CC BY-SA 4.0
  19. // ==/UserScript==
  20.  
  21. var currentLoc = window.location.href;
  22. var lastLoc = "";
  23.  
  24. var mainInterval = setInterval(function (timer) {
  25. currentLoc = window.location.href;
  26.  
  27. // /// Print currentt location
  28. // window.console.log(currentLoc);
  29.  
  30. /// Only run on change of Window Location
  31. if (currentLoc != lastLoc) {
  32. /// Only run on video link
  33. if (RegExp("^https://(w{3}|m).youtube.com/watch").test(currentLoc)) {
  34. /// Try to click the open chapter Button for 3.0 seconds
  35. var run = 0;
  36. var secondaryInterval = setInterval(function (timer) {
  37. run++;
  38. if (run <= 16) {
  39. document.querySelector(".ytp-chapter-title-content")?.click();
  40. } else {
  41. // timer.cancel();
  42. clearInterval(secondaryInterval);
  43. }
  44. }, 250);
  45. }
  46. }
  47.  
  48. lastLoc = currentLoc;
  49. }, 250);