Return to Snippet

Revision: 8456
at March 10, 2010 13:31 by wizard04


Updated Code
// ==UserScript==
// @name          Fix Snippet's Plain/Highlighted Text link
// @author        Andy Harrison
// @namespace     http://dragonzreef.com/greasemonkey/snipplr_plainhighlighted.user.js
// @description   When viewing a snippet, fixes the Plain/Highligted Text link to go to the version you're looking at instead of the latest version.
// @include       http://snipplr.com/view*
// ==/UserScript==

window.addEventListener("load", function()
	{
		var viewbar = document.getElementById("viewsource");
		if(!viewbar) return;
		var a = viewbar.getElementsByTagName("a");
		for(var i=0; i<a.length; i++)
		{
			if(a[i].innerHTML == "Plain Text")
			{
				a[i].href += document.location.href.replace(/^http:\/\/snipplr.com\/view\/\d+(\.\d+).*$|.*/,"$1");
				return;
			}
			else if(a[i].innerHTML == "Highlighted Text")
			{
				var vers = document.location.href.replace(/^http:\/\/snipplr.com\/view.php\?([^#]*&)*?id=\d+(\.\d+).*$|.*/,"$2");
				a[i].href = a[i].href.replace(/^(http:\/\/snipplr.com\/view\/\d+)(.*)$/, "$1"+vers+"$2");
				return;
			}
		}
	}, false);

Revision: 8455
at September 23, 2008 12:33 by wizard04


Updated Code
// ==UserScript==
// @name          Fix Snippet's Plain/Highlighted Text link
// @author        Andy Harrison
// @namespace     http://dragonzreef.com/greasemonkey/snipplr_plainhighlighted.user.js
// @description   When viewing a snippet, fixes the Plain/Highligted Text link to go to the version you're looking at instead of the latest version.
// @include       http://snipplr.com/view*
// ==/UserScript==

fixViewbarLink();

function fixViewbarLink()
{
	var viewbar = document.getElementById("viewsource");
	if(!viewbar) return;
	var toggle = getDescendantsByPath(viewbar, "div.sourcenav span.rgt a")[0];
	if(!toggle) return;
	var here = document.location.href;
	var rxp = /^http:\/\/snipplr\.com\/view\/([0-9.]+)\//i;
	var snippetID;
	if(rxp.test(here))
	{
		snippetID = here.replace(rxp, "$1");
		//fix Plain Text link
		toggle.href = "http://snipplr.com/view.php?codeview&id="+snippetID;
	}
	else
	{
		rxp = /^http:\/\/snipplr\.com\/view\.php\?codeview&id=([0-9.]+)/i;
		if(!rxp.test(here)) return;
		snippetID = here.replace(rxp, "$1");
		//fix Highlighted Text link
		toggle.href = "http://snipplr.com/view/"+snippetID+"/";
	}
}



//***** DOM traversal functions ***//

function getChildElements(obj)
{
	var children = obj.childNodes;
	var elems = [];
	if(children)
	{
		for(var i=0; i<children.length; i++)
		{
			if(children[i].nodeType == document.ELEMENT_NODE)	//it's an element node
			{
				elems.push(children[i]);
			}
		}
	}
	return elems;
}

//get elements with a path such as "div.class1 span.class2a.class2b * .class3"
function getDescendantsByPath(obj, path)
{
	var results = [];
	
	if(!path) return results;
	var node = path.split(" ")[0];
	path = path.slice(node.length+1);
	var attrs = node.split(".");
	if(!attrs) return results;
	
	var elems = getChildElements(obj);
	var rexp;
	var j;
	for(var i=0; i<elems.length; i++)
	{
		if(attrs[0] && attrs[0] != "*" && elems[i].nodeName.toLowerCase() != attrs[0]) continue;	//if a tag name was specified but it doesn't match
		for(j=1; j<attrs.length; j++)
		{
			if(!attrs[j]) continue;
			rexp = new RegExp("(^|\\s)"+attrs[j]+"(\\s|$)");
			if(!rexp.test(elems[i].className)) break;	//one of the classes was not found
		}
		if(j == attrs.length)	//if elems[i].className includes all the classes
		{
			if(path)	//if there is more to the path
				results = results.concat(getDescendantsByPath(elems[i], path));
			else
				results.push(elems[i]);
		}
	}
	return results;
}

Revision: 8454
at September 23, 2008 12:20 by wizard04


Initial Code
// ==UserScript==
// @name          Fix Snippet's Plain/Highlighted Text link
// @author        Andy Harrison
// @namespace     http://dragonzreef.com/namespace/snipplr_fixviewbarlink
// @description   When viewing a snippet, fixes the Plain/Highligted Text link to go to the version you're looking at instead of the latest version.
// @include       http://snipplr.com/view*
// ==/UserScript==

fixViewbarLink();

function fixViewbarLink()
{
	var viewbar = document.getElementById("viewsource");
	if(!viewbar) return;
	var toggle = getDescendantsByPath(viewbar, "div.sourcenav span.rgt a")[0];
	if(!toggle) return;
	var here = document.location.href;
	var rxp = /^http:\/\/snipplr\.com\/view\/([0-9.]+)\//i;
	var snippetID;
	if(rxp.test(here))
	{
		snippetID = here.replace(rxp, "$1");
		//fix Plain Text link
		toggle.href = "http://snipplr.com/view.php?codeview&id="+snippetID;
	}
	else
	{
		rxp = /^http:\/\/snipplr\.com\/view\.php\?codeview&id=([0-9.]+)/i;
		if(!rxp.test(here)) return;
		snippetID = here.replace(rxp, "$1");
		//fix Highlighted Text link
		toggle.href = "http://snipplr.com/view/"+snippetID+"/";
	}
}



//***** DOM traversal functions ***//

function getChildElements(obj)
{
	var children = obj.childNodes;
	var elems = [];
	if(children)
	{
		for(var i=0; i<children.length; i++)
		{
			if(children[i].nodeType == document.ELEMENT_NODE)	//it's an element node
			{
				elems.push(children[i]);
			}
		}
	}
	return elems;
}

//get elements with a path such as "div.class1 span.class2a.class2b * .class3"
function getDescendantsByPath(obj, path)
{
	var results = [];
	
	if(!path) return results;
	var node = path.split(" ")[0];
	path = path.slice(node.length+1);
	var attrs = node.split(".");
	if(!attrs) return results;
	
	var elems = getChildElements(obj);
	var rexp;
	var j;
	for(var i=0; i<elems.length; i++)
	{
		if(attrs[0] && attrs[0] != "*" && elems[i].nodeName.toLowerCase() != attrs[0]) continue;	//if a tag name was specified but it doesn't match
		for(j=1; j<attrs.length; j++)
		{
			if(!attrs[j]) continue;
			rexp = new RegExp("(^|\\s)"+attrs[j]+"(\\s|$)");
			if(!rexp.test(elems[i].className)) break;	//one of the classes was not found
		}
		if(j == attrs.length)	//if elems[i].className includes all the classes
		{
			if(path)	//if there is more to the path
				results = results.concat(getDescendantsByPath(elems[i], path));
			else
				results.push(elems[i]);
		}
	}
	return results;
}

Initial URL


Initial Description
When you're viewing an older version of a snippet, the Plain Text and Highlighted Text links point back to the latest version.  This greasemonkey script fixes those links to point to the version you're currently viewing.

Initial Title
Snipplr Plain/Highlighted Text Greasemonkey Script

Initial Tags
javascript

Initial Language
JavaScript