Youtube clickbait capitalization fix

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

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 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);
})();