Youtube clickbait capitalization fix

Makes all Youtube video titles consistent, only the first letter of the title is uppercase

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Youtube clickbait capitalization fix
// @namespace    Youtube
// @version      2023-12-18
// @match https://*.youtube.com
// @description  Makes all Youtube video titles consistent, only the first letter of the title is uppercase
// @author       You
// @icon         data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==
// @grant        none
// @license MIT
// ==/UserScript==


function formatTitle(element) {
    // Get the title attribute of the parent element
    let titleText = element.parentNode.getAttribute('title');

    // Split the title into words
    let words = titleText.split(' ');

    // Capitalize the first word and make the rest lowercase
    words = words.map((word, index) => {
        if (index === 0) {
            return word.charAt(0).toUpperCase() + word.slice(1).toLowerCase();
        } else {
            return word.toLowerCase();
        }
    });

    // Join the words back together
    let formattedTitle = words.join(' ');

    // Set the text content of the element to the formatted title
    element.textContent = formattedTitle;
}

function observeTitleChanges() {
    // Get all elements with the tag <yt-formatted-string> and the id #video-title
    let titleElements = document.querySelectorAll('yt-formatted-string#video-title');

    // Create a MutationObserver instance
    let observer = new MutationObserver((mutations) => {
        mutations.forEach((mutation) => {
            if (mutation.type === 'attributes' && mutation.attributeName === 'title') {
                formatTitle(mutation.target.querySelector('yt-formatted-string#video-title'));
            }
        });
    });

    // Start observing each parent element for changes in its title attribute
    titleElements.forEach(titleElement => {
        observer.observe(titleElement.parentNode, { attributes: true, attributeFilter: ['title'] });
        formatTitle(titleElement);
    });
}



(function() {
    'use strict';

// Set up a timer to check continuously if the titles have loaded
let checkInterval = setInterval(() => {
    if (document.querySelectorAll('yt-chip-cloud-chip-renderer').length > 5) {
        clearInterval(checkInterval);
        observeTitleChanges();
    }
}, 500);
})();