Javascript Link Fixer

Converts Javascript links that open new windows into regular old links

Versione datata 10/01/2016. Vedi la nuova versione l'ultima versione.

Dovrai installare un'estensione come Tampermonkey, Greasemonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Userscripts per installare questo script.

Dovrai installare un'estensione come ad esempio Tampermonkey per installare questo script.

Dovrai installare un gestore di script utente per installare questo script.

(Ho già un gestore di script utente, lasciamelo installare!)

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

(Ho già un gestore di stile utente, lasciamelo installare!)

// ==UserScript==
// @name          Javascript Link Fixer
// @namespace     DoomTay
// @description   Converts Javascript links that open new windows into regular old links
// @version       1.1.0
// @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 arguments = /\((\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 < arguments.length; a++)
			{
				statementParams = statementParams.replace(new RegExp(" ?\\+ ?" + arguments[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 == arguments[p]) return params[p];
			}
		}
	}
	return null;
}