Wes_Reimer / CBC Music Cleanup & Comment Link

// ==UserScript==
// @name        	CBC Music Cleanup & Comment Link
// @description		Hides header and footer elements, fixes the player position to the top of the screen, adds [Reply] shortcut to each comment and automatically links comment references in the format #xxx.
// @id 				cbc-cleanup-commentlink
// @version			1.7
// @author      	Wes Reimer 
// @contributor 	Geoff Appleby
// @namespace		http://reimer-reason.ca
// @screenshot		http://reimer-reason.ca/cbc/cbc_comments.png
// @include			http://music.cbc.ca/*

// ==/UserScript==


(function(doc) {

var i = 0,
    
    getEl = function(id) {
    	return doc.getElementById(id);
    },
    
	hide = function(id) {
    	var div = getEl(id);
    	if (div) div.style.display = 'none';
	},
    
    replyFn = function() { 
    	var range, sel, iDoc,
    		iframe = getEl('ctl00_LeftColumn_ctlPostComment_ctlEditor_ctlTextEditor_ifr');
    	
    	if (iframe) {
    		iDoc = iframe.contentDocument;
    		iDoc.body.innerHTML = this.data;
    		iframe.focus();
    		range = iDoc.createRange();//Create a range (a range is a like the selection but invisible)
        	range.selectNodeContents(iDoc.body);//Select the entire contents of the element with the range
        	range.collapse(false);//collapse the range to the end point. false means collapse to end rather than the start
        	sel = iframe.contentWindow.getSelection();//get the selection object (allows you to change selection)
        	sel.removeAllRanges();//remove any selections already made
        	sel.addRange(range);//make the range you have just created the visible selection
    	}
    }, 
    
    linkComments = function() {
    
        var a, j, id, name, div, match, commentP, comment, text, num, lastIndex,
        	container = doc.querySelectorAll('.commentContainer'),
        	rx = /[#@\[](\d+)]?/g;
            
        for (; i<container.length; i++) { // loop over each comment
        
        	// find comment references 
            text = container[i].querySelector('.blogCommentText');
            match = text.textContent.match(rx);
            if (match) { //console.log(match);
                for (j = 0; j<match.length; j++) {
                    num = match[j].replace(rx, '$1'); // get just the number
                    commentP = doc.querySelectorAll('#comment-' + num + ' .blogCommentText p');
                    if (commentP.length) {  //matching comment found
                    	comment = commentP[0].textContent;
                    	if (commentP.length > 1) {
                    		comment += ('\n\n' + commentP[1].textContent);
                    		if (commentP.length > 2) {
                    			comment += ' [...]';
                    		}
                    	}
                        comment = comment.replace(/"/g, '&quot;');
                        text.innerHTML = text.innerHTML.replace(rx, '<a href="#comment-$1" title="' + comment
                        	 + '" onclick="document.getElementById(\'comment-$1\').style.backgroundColor=\'#FFA\'">'
                        	 + match[j] + '</a>');
                    }
                } // end for
            } 
          
          
          	// add Reply link
            div = container[i].querySelector('.blogCommentImage div');
            id = div.textContent.match(rx);
            if (!id) return;
            name = div.querySelector('a').textContent.trim();
            a = doc.createElement('a');
            a.data = '@' + name + ' ' + id[0] + ',&nbsp;';
            a.href='#postCommentAnchor';
            a.onclick = replyFn;
            a.innerHTML='[Reply]';
            a.style.cssFloat='right';
            text.appendChild(a);
        }
    };

// check for new comments every 9 seconds
setInterval(linkComments,9000); 


// Hide stuff
hide('footer');
hide('cbcFooter');
hide('leaderboard');
hide('navHeaderWrapperDiv');

// Float the music/radio player
var s, 
    div = getEl('r3player');
if (div) {
    s = div.style;
    s.zIndex = 100;
    s.position = 'fixed';
	s.top = '10px';
}


})(document);