// ==UserScript==
// @name          Youtube blablabla
// @namespace     http://jpmullan.com
// @description   transform YouTube comments into reasonable and smart ones
// @include       http://*youtube*/*
// ==/UserScript==

// Based on feyntube, Written 2008 by Julien Oster <feyntube@julien-oster.de>,
// find newest version at http://www.julien-oster.de/projects/feyntube

function blanode(node) {
    var text = "";
    // if the node has children, loop through them
    if (node.hasChildNodes()) {
	var children = node.childNodes;
	for (var i=0; i < children.length; i++) {
	    blanode(children[i]);
	}
    } else if (node.nodeName == "#text") {
	node.oldText = node.nodeValue;
	node.newText = blatext(node.nodeValue);
	node.nodeValue = node.newText;
	/* Uncomment the following if you hate life only a little and want a
	 * reason to end it, like the ability to see the source text of
	 * comments.  I do not recommend this course of action, but it is there.
	 */
	/*
	  node.parentNode.addEventListener('mouseover', function (event) {
	  node.nodeValue = node.oldText;
	  }, false)
	  node.parentNode.addEventListener('mouseout', function (event) {
	  node.nodeValue = node.newText;
	  }, false);
	*/
    }
    return text;
}
			    
function blatext(input_string) {
    /* split the input on word boundaries */
    var input_parts = input_string.split(/\b/);
    var output_string = '';
    var part;
    for (var j = 0; j < input_parts.length; j++) {
	part = input_parts[j];
	if (part.match(/^[A-Z]+$/)) {
	    /* words that are all caps */
	    output_string += 'BLA';
	} else if (part.match(/^[A-Z]\w+$/)) {
	    /* words that are capitalized */
	    output_string += 'Bla';
	} else if (part.match(/^[a-z][a-z]+$/)) {
	    /* words that are all lowercase and have more than one character
	     * this leave possessives and the like intact, so you get bla's
	     * instead of bla'bla, which is the wrong kind of funny */
	    output_string += 'bla';
	} else if ('I' == part) {
	    /* self reference?  whatever */
	    output_string += 'Bla';
	} else if (part.match(/^[0-9]+th$/)) {
	    /* fix ordinal numbers! */
	    output_string += 'blath';
	} else if (part.match(/^[0-9]+rd$/)) {
	    output_string += 'blard';
	} else if (part.match(/^[0-9]+st$/)) {
	    output_string += 'blast';
	} else {
	    /* Okay, that's plenty.  Anything else would be just mean. */
	    output_string += part;
	}
    }
    return output_string;
}

var allDivs = document.getElementsByTagName('div');
for (var i = 0; i < allDivs.length; i++) {
    div = allDivs[i];
    if (div.hasAttribute('class')
	&& div.getAttribute('class').match(/comment-body/)) {
	blanode(div);
    }
}

