// ==UserScript==
// @name          Stright Click
// @namespace     http://www.arantius.com/misc/greasemonkey/
// @description	  Click straight through links that just redirect somewhere else.
//                Based on "NoMiddleMan" from http://0x539.blogspot.com/
// @include       *
// @exclude       http://del.icio.us/*
// @exclude       http://web.archive.org/*
// @exclude       http://*googlesyndication.com*
// ==/UserScript==

(function(){
var atGoogSearch=true&&document.location.href.match(/www.google.com\/search/);

var href=null, uhref=null, start=null, end=null, j=0;
for (var i=0, el=null; el=document.links[i]; i++) {
	if (j++>500) return; //failsafe against infinite loops

	//special case to allow links to google cache through
	if (el.href.match(/\/search\?q=cache/)) continue;
	
	//special case for google search.  rewriting links on click, EVIL !!!
	if (atGoogSearch) el.removeAttribute('onmousedown');

	//grab the link and strip off the protocol in front
	href=el.href;
	if ('http://'!=href.substr(0, 7)) continue;
	href=href.substr(7);

	//if there's no other link embedded in this link, forget it
	uhref=unescape(href);
	start=uhref.indexOf('http:');
	if (-1==start) start=uhref.indexOf('www.');
	if (0>=start) continue;

	//at this point we've found a new sub link, grab it out
	//(re-evaluate on non-unescaped string)
	start=href.indexOf('http');
	if (-1==start) start=href.indexOf('www.');
	if (0>=start) continue;
	href=href.substr(start);
	if (href.indexOf('&')>0) href=href.substr(0, href.indexOf('&'));
	href=unescape(href);
	if ('http://'==href.substr(0, 7)) href=href.substr(7);

	//permit links that contain sub-links to the same domain we're at
	var host=href.substr(0, href.indexOf('/'));
	if (-1!=host.indexOf(':')) host=host.substr(0, host.indexOf(':'));
	if (host==document.location.host) continue;

	//rewrite this link
	//dump('Rewrote: '+el.href+'\nTo:      http://'+href+'\n');
	el.href='http://'+href;

	//in case this new link has a sub-link, decrement i so it gets re-evaluated
	i--;
}
})();
