Javascript Link Fixer

Converts Javascript links that open new windows into regular old links

Fra og med 10.01.2016. Se den nyeste version.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

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

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

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

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

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name          Javascript Link Fixer
// @namespace     DoomTay
// @description   Converts Javascript links that open new windows into regular old links
// @version       1.1.2
// @grant         none

// ==/UserScript==

var links = document.querySelectorAll("a[href *= 'javascript:']");

var paramTable = {};
	paramTable["open"] = 0;

for (var l = 0; l < links.length; l++)
{
	if(links[l].href.indexOf("void") > -1) continue;
	var hrefSplitter = /javascript:(\S+)\((\S+(?:,(?: )\S+)*)?\)/.exec(links[l].href);
	if(hrefSplitter == null) continue;
	var functionName = hrefSplitter[1];
	var params = hrefSplitter[2].split(",");
	if(deconstructFunction(functionName,params) != null) links[l].href = deconstructFunction(functionName,params);
}

function deconstructFunction(functionName,params)
{
	var sourceCode = uneval(window[functionName]);
	var functionBody = sourceCode.substring(sourceCode.indexOf("{") + 1,sourceCode.indexOf("}")).trim();
	var args = /\((\S+(?:,(?: )\S+)*)?\)/.exec(sourceCode)[1].split(/\s*,\s*/);
	var containedFunctions = (/(\S+)\(/g.exec(functionBody));
	if(containedFunctions)
	{
		containedFunctions.shift();
		for(var s = 0; s < containedFunctions.length; s++)
		{
			var statementName = containedFunctions[s];
			var statementParams = new RegExp(statementName + "\\(((?:.+|\".+\"))\\);").exec(functionBody)[1];
			for(var a = 0; a < params.length; a++)
			{
				statementParams = statementParams.replace(new RegExp(" ?\\+ ?" + args[a],"g"), " + " + params[a]);
				statementParams = statementParams.replace(new RegExp(args[a],"g"), params[a]);
			}
			statementParams = eval("[" + statementParams + "]");
			if(paramTable.hasOwnProperty(statementName)) return(statementParams[paramTable[statementName]]);
			var digDeeper = deconstructFunction(statementName,statementParams);
			if(digDeeper) return digDeeper;
		}
	}
	else
	{
		if(functionBody.indexOf(".location = ") > -1)
		{
			var test = /\.location ?= ?(\S+);/.exec(functionBody)[1];
			for(var p = 0; p < params.length; p++)
			{
				if(test == args[p]) return params[p];
			}
		}
	}
	return null;
}