RedRem

Converts links containing redirects to their resolved version.

目前為 2015-06-24 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         RedRem
// @namespace    http://www.twprogrammers.com/
// @version      0.5
// @description  Converts links containing redirects to their resolved version.
// @grant        none
// @include      http://www.free-tv-video-online.info/*
// @include      http*://www.webmasterworld.com/*
// @include      http://www.researchgate.net/*
// @include      https://*.google.*
// @exclude      http://www.free-tv-video-online.info/player/*
// ==/UserScript==

var orig_page_links = document.links;
var page_links = new Array(orig_page_links.length);
var newlinkhref, pglh, query, queryEncoded, tArray, tkvArray, i;

function cleanGoogle(e)
{
	var a = anchorMatch(e.target);
	if(a && a.localName == "a")
	{
		var m = a.getAttribute("onmousedown");
		var h = a.getAttribute("href");
		if(m && m.indexOf("return") === 0)
		{
			a.removeAttribute("onmousedown");
		}
		else if(h)
		{
			if(h.indexOf("http://") === 0) h = h.substr(h.indexOf("/", 7));
			if(h.indexOf("/url?") === 0)
			{
				h = h.substr(5).toObj();
				a.setAttribute('href', decodeURIComponent(h.url || h.q));
				a.setAttribute('rel', 'noreferrer');
			}
		}
	}
}

function anchorMatch(a)
{
	for(; a; a = a.parentNode) if(a.localName == 'a') return a;
	return null;
}
function getQueryParams(qs)
{
	query = {};
	qs = qs.substring(qs.indexOf('?')+1);
	if(getContains("&", qs))
	{
		tArray = qs.split("&");
		for(i = 0; i < tArray.length; i++)
		{
			tkvArray = tArray[i].split("=");
			query[tkvArray[0]] = decodeURIComponent(tkvArray[1]);
		}
	}
	else
	{
		tkvArray = qs.split("=");
		query[tkvArray[0]] = decodeURIComponent(tkvArray[1]);
	}
	return query;
}
function getQueryParamsEncoded(qs)
{
	queryEncoded = {};
	qs = qs.substring(qs.indexOf('?')+1);
	if(getContains("&", qs))
	{
		tArray = qs.split("&");
		for(i = 0; i < tArray.length; i++)
		{
			tkvArray = tArray[i].split("=");
			queryEncoded[tkvArray[0]] = tkvArray[1];
		}
	}
	else
	{
		tkvArray = qs.split("=");
		queryEncoded[tkvArray[0]] = tkvArray[1];
	}
	return queryEncoded;
}
function getContains(needle, haystack)
{
	return haystack.match(new RegExp(".*("+needle.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&")+").*", "i"));
}
function directify()
{
	for (i=0; i < page_links.length; i++)
	{
		page_links[i] = orig_page_links[i];
	}
	for (i=0; i < page_links.length; i++)
	{
		pglh = page_links[i].href;
		getQueryParams(pglh);
		getQueryParamsEncoded(pglh);
		if (getContains("free-tv-video-online.info/interstitial2.html", pglh))
		{
			newlinkhref = query.lnk;
			console.log(page_links[i].href + " => " + newlinkhref);
			page_links[i].href = newlinkhref;
		}
		else if(pglh.match(/webmasterworld\.com(\/.*)?/i))
		{
			newlinkhref = queryEncoded.url;
			console.log(page_links[i].href + " => " + newlinkhref);
			page_links[i].href = newlinkhref;
		}
		else if(getContains("www.researchgate.net/go.Deref.html", pglh))
		{
			newlinkhref = query.url;
			console.log(page_links[i].href + " => " + newlinkhref);
			page_links[i].href = newlinkhref;
		}
		else if(getContains("google", document.location.href))
		{
			addEventListener("mousedown", cleanGoogle, true);
		}
	}
}
directify();