YouTube Chapter Title to Page Title

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

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         YouTube Chapter Title to Page Title
// @namespace    https://greasyfork.org/en/users/1413127-tumoxep
// @version      1.0
// @description  Displays the current chapter title of a YouTube video in the page title, scrolling if necessary.
// @match        https://www.youtube.com/watch*
// @license      WTFPL
// @grant        none
// ==/UserScript==

(function () {
    'use strict';

    let scrollIndex = 0;
    let currentChapterTitle = '';
    const isScrollingEnabled = true;

    function updateTitle() {
        let chapterElement = document.querySelector('.ytp-chapter-title-content');
        if (!chapterElement?.textContent) {
            chapterElement = document.querySelector("#title yt-formatted-string");
        }
        if (chapterElement) {
            const newTitle = chapterElement.textContent.trim();
            if (newTitle !== currentChapterTitle) {
                currentChapterTitle = newTitle;
                scrollIndex = 0;
            }

            const maxLength = 20; // Adjust based on your preference
            if (currentChapterTitle.length > maxLength && isScrollingEnabled) {
                const displayedTitle = currentChapterTitle.substring(scrollIndex, scrollIndex + maxLength);
                document.title = displayedTitle;

                scrollIndex++;
                if (scrollIndex > currentChapterTitle.length - maxLength) {
                    scrollIndex = 0;
                }
                setTimeout(updateTitle, 600); // this one too
                return;
            } else {
                document.title = currentChapterTitle;
            }
        }
        setTimeout(updateTitle, 2000); // and this one
    }

    updateTitle();
})();