YouTube Chapter Title to Page Title

Displays the current chapter title of a YouTube video in the page title, scrolling if necessary.

  1. // ==UserScript==
  2. // @name YouTube Chapter Title to Page Title
  3. // @namespace https://greasyfork.org/en/users/1413127-tumoxep
  4. // @version 1.0
  5. // @description Displays the current chapter title of a YouTube video in the page title, scrolling if necessary.
  6. // @match https://www.youtube.com/watch*
  7. // @license WTFPL
  8. // @grant none
  9. // ==/UserScript==
  10.  
  11. (function () {
  12. 'use strict';
  13.  
  14. let scrollIndex = 0;
  15. let currentChapterTitle = '';
  16. const isScrollingEnabled = true;
  17.  
  18. function updateTitle() {
  19. let chapterElement = document.querySelector('.ytp-chapter-title-content');
  20. if (!chapterElement?.textContent) {
  21. chapterElement = document.querySelector("#title yt-formatted-string");
  22. }
  23. if (chapterElement) {
  24. const newTitle = chapterElement.textContent.trim();
  25. if (newTitle !== currentChapterTitle) {
  26. currentChapterTitle = newTitle;
  27. scrollIndex = 0;
  28. }
  29.  
  30. const maxLength = 20; // Adjust based on your preference
  31. if (currentChapterTitle.length > maxLength && isScrollingEnabled) {
  32. const displayedTitle = currentChapterTitle.substring(scrollIndex, scrollIndex + maxLength);
  33. document.title = displayedTitle;
  34.  
  35. scrollIndex++;
  36. if (scrollIndex > currentChapterTitle.length - maxLength) {
  37. scrollIndex = 0;
  38. }
  39. setTimeout(updateTitle, 600); // this one too
  40. return;
  41. } else {
  42. document.title = currentChapterTitle;
  43. }
  44. }
  45. setTimeout(updateTitle, 2000); // and this one
  46. }
  47.  
  48. updateTitle();
  49. })();