ben25 / YouTube Caps

// ==UserScript==
// @name         YouTube Caps
// @namespace    http://tampermonkey.net/
// @version      0.3
// @description  Changes words with all caps in the youtube video title to only first letter in uppercase
// @author       Benjamin
// @match        https://www.youtube.com/
// @match        https://*.youtube.com/*
// @grant        none
// @require      http://code.jquery.com/jquery-latest.js
// ==/UserScript==

(function() {
    'use strict';
    //changeAllTitles();
})();

CAPS_TITLE_SELECTORS = ['[id="video-title"]', "h1.title", "title"];

startMain();
function startMain() {
	var observer = new MutationObserver(mutationHandler);
	var titleObserver = new MutationObserver(changeMutationHandler);
	var config = { childList: true, subtree: true};
	var configWithAttributes = { childList: false, subtree: true};
	observer.observe(document, config);
	titleObserver.observe(document.querySelector("title"), config);
    document.title = changeUpperCaseToOnlyFirstUpper(document.title);
}

function mutationHandler (mutationRecords) {
    mutationRecords.forEach ( function (mutation) {
        if (typeof mutation.addedNodes == "object") {
            var addedNodes = $(mutation.addedNodes);
            changeAllTitles(addedNodes);
        }
    } );
}
function changeMutationHandler (mutationRecords) {
    mutationRecords.forEach ( function (mutation) {
        if (typeof mutation.target == "object") {
            var elList = $(mutation.target);
            var element = elList[0];
            if(!isNormalized(element.innerHTML)){
                element.innerHTML =  changeUpperCaseToOnlyFirstUpper(element.innerHTML);
            }
        }
    } );
}

function changeAllTitles(addedNodes){
    CAPS_TITLE_SELECTORS.forEach(function(selector) {
         objectsToChange = addedNodes.find(selector);
        if(objectsToChange.length > 0){
            objectsToChange.each(function(index, element){
                if(!isNormalized(element.innerHTML)){
                    var newTitle = changeUpperCaseToOnlyFirstUpper(element.innerHTML);
                    element.innerHTML = newTitle;
                }
            });
        }
    });
    return;
}


function reformatInnerHTMLFromXpath(xpath) {
    var snapResults = document.evaluate(xpath, document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
    console.log(xpath+" : "+snapResults.snapshotLength);
     for (var i = snapResults.snapshotLength - 1; i >= 0; i--) {
		var el = snapResults.snapshotItem(i);
        var newTitle = changeUpperCaseToOnlyFirstUpper(el.innerHTML);
        el.innerHTML = newTitle;
	}
    return;
}

function reformatInnerHTMLFromCSS(css) {
    var snapResults = $$(css);
    console.log(css+" : "+snapResults.length);
     for (var i = snapResults.length - 1; i >= 0; i--) {
		var el = snapResults[i];
        var newTitle = changeUpperCaseToOnlyFirstUpper(el.innerHTML);
        el.innerHTML = newTitle;
	}
    return;
}

function changeUpperCaseToOnlyFirstUpper(text){

    var titleWords = text.split(" ");
        for (var j = titleWords.length - 1; j >= 0; j--) {
          var title = titleWords[j];
          if (title === title.toUpperCase()){
              title = title.charAt(0).toUpperCase() + title.slice(1).toLowerCase();
		      titleWords[j] = title;
          }
        }
     return titleWords.join(" ");
}

function isNormalized(text){
    var newText = changeUpperCaseToOnlyFirstUpper(text);
    return text == newText;
}


$(window).load(function() {
   changeAllTitles($(document));
});