Reddit parent comment

By lazyttrick Last update Jun 23, 2009 — Installed 1,092 times. Daily Installs: 3, 4, 0, 0, 0, 2, 2, 0, 1, 0, 4, 2, 1, 0, 3, 1, 173, 49, 15, 4, 1, 1, 7, 2, 2, 1, 2, 3, 3, 1, 4, 4

There are 7 previous versions of this script.

// ==UserScript==
// @name           Reddit parent comment 
// @namespace      Reddit parent comment
// @description    Mouse over "parent" link to show parent comment.
// @include        http://www.reddit.com/r/*/comments/*
// ==/UserScript==


xp('//div[starts-with(@class,"entry")]//ul[@class="flat-list buttons"]/li[2]/a').forEach(function(a){
	a.addEventListener('mouseover', show, false);
	a.addEventListener('mouseout', hide, false);
});


function show(evt)
{
	var id = evt.target.getAttribute('href').replace(/\#/,"");
	var top = parseInt(evt.pageY,10)+10, 
		left = parseInt(evt.pageX,10)+10;
	try{
		var div = createElement('div', {id:"parentComment"+id, style:"padding:5px; border: 1px solid #666666; overflow:auto; background:#F8F8F8; position:absolute; top:"+top+"px; left:"+left+"px;"}, null );
		var query = "//div[contains(@class, 'id-t1_"+id+"')]/div[starts-with(@class,'entry')]";
		div.innerHTML = xp(query)[0].innerHTML.replace(/\<ul\s+class[\s\S]+\<\/ul\>/,"").replace(/\<a[^\>]+>\[-\]\<\/a\>/,'');
		getTag('body')[0].appendChild(div);
	}catch(e){
	}
}


function hide(evt)
{
	var id = evt.target.getAttribute('href').replace(/\#/,"");
	try{
		var div = getId("parentComment"+id);
		div.parentNode.removeChild(div);
	}catch(e){
	}
}


function xp(p, context) {
  if (!context) 
	context = document;
  var i, arr = [], xpr = document.evaluate(p, context, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
  for (i = 0; item = xpr.snapshotItem(i); i++) 
	arr.push(item);
  return arr;
}


function createElement(type, attrArray, evtListener, html)
{
	var node = document.createElement(type);

	for (var attr in attrArray) if (attrArray.hasOwnProperty(attr)){
		node.setAttribute(attr, attrArray[attr]);
	}

	if(evtListener){
		var a = evtListener.split(' ');
		node.addEventListener(a[0], eval(a[1]), eval(a[2]));
	} 
 
	if(html) 
		node.innerHTML = html;
	
	return node;
}

function getId(id, parent){
	if(!parent)
		return document.getElementById(id);
	return parent.getElementById(id);	
}

function getTag(name, parent){
	if(!parent)
		return document.getElementsByTagName(name);
	return parent.getElementsByTagName(name);
}


//TODO:  print debugObj with added variables
function debug(str)
{
	var d = document.getElementById('debuggg');
	if(!d) {
		d = document.createElement('textarea');
		d.setAttribute('id','debuggg');
		d.setAttribute('style',"height:200px; width:50%; position:fixed; bottom:0px; right:0px;");
		document.body.appendChild(d);
	}
	d.innerHTML += '\n' + str;
	d.scrollTop = d.scrollHeight;
}