// Script to fix really stupid things that webmasters do
// Version 4
// Copyright 2005, 2007 Adam Sampson <ats@offog.org>
//
// History:
// 1: initial release
// 2: added target=new (which synthfool.com uses)
// 3: added link redirect removal
// 4: added <base target="_blank"> (which r-type.org uses)
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
// ==UserScript==
// @name	No Stupid Webmasters
// @namespace	http://offog.org/code/greasemonkey/
// @description	Fix some stupid things that webmasters do
// @include	*
// ==/UserScript==

var adRE = new RegExp("^http://[^/]*/click-[0-9]+-[0-9]+\\?loc=(.*)$");
var decodeRE = new RegExp("^http%3A");

function is_new_window(t) {
	return t == "_blank" || t == "_new" || t == "new";
}

function process(a) {
	// Remove irritating "open in new window" links.
	if (is_new_window(a.target))
		a.removeAttribute("target");
	if (is_new_window(a._base_target))
		a.removeAttribute("_base_target");

	// Rewrite links that redirect via an advertising service.
	var href = a.href;
	var m = adRE.exec(href);
	if (m) {
		var realLink = m[1];
		if (decodeRE.test(realLink)) {
			realLink = decodeURIComponent(realLink);
		}
		a.href = realLink;
	}
}

var all = document.getElementsByTagName("a");
for (var i = 0; i < all.length; i++)
	process(all[i]);

all = document.getElementsByTagName("base");
for (var i = 0; i < all.length; i++)
	process(all[i]);


