User:MrPatan/indexer.js

(function(w, console, mw, $, undefined){ var doc = w.document;  var host = function(hostname, a){a.hostname=hostname};  var frameableDomains = {    "en.wikipedia.org" : host.bind({}, "en.m.wikipedia.org"),     "www.wikia.com" : function(a){a.href+="?useskin=wikiamobile";},    ".wikia.com" : function(a){a.href+="?useskin=wikiamobile";},    "www.wikihow.com" : host.bind({}, "m.wikihow.com")  };  function processPage{    if(doc.getElementById('mw-content-text')){      doc.body.className += " with-indexer-active";    }    augmentReferences;  }

function augmentReferences{ $(".reference a").each(function{		var ref = $(this);		var p = ref.closest("p,pre");		if (!p || p.length===0){			p = ref.parent;			return;		}		var position = 0;		p.find(".reference a").each(function(ind){ if($(this).attr("id") === ref.attr("id")){ position = ind; }		});		p.html(p.html.replace(new RegExp("(.*"+ref.parent.attr("id")+".*?\/sup[>])(.*)"),			"$1 $2"));		var span = p.find("span.indexer-reference-p");		var decorateP = false;		if(decorateP){			if (p.hasClass("indexer-reference-p")) {				p[0].className = p[0].className.replace(/(indexer-reference-p indexer-reference-p-i+)/,"$1i");			} else {				p.addClass("indexer-reference-p indexer-reference-p-i");			}		}		$(" ")			.addClass("indexer-reference-popup")			.append($("  ").addClass("indexer-reference-popup-original-text").html($(ref[0].hash).find(".reference-text").html))			.appendTo(span);    }); $(".indexer-reference-p").each(function{		var top = 0;		var popups = $(this).find(".indexer-reference-popup");		popups.each(function{ this.style.top = top+"px"; // top += 16 + this.offsetHeight; });		// var heightp = this.offsetHeight;		popups.each(function{ // this.style.top = (parseInt(this.style.top.replace(/px/,""),10)+(heightp-top)/2)+"px"; var popup = $(this); var frameName = "indexer-iframe-"+popup.attr("id"); popup.find("a").each(function{				var a = $(this);				var matched = Object.keys(frameableDomains).filter(function(key){return new RegExp(key).test(a.hostname);});				if(matched && matched.length){					a.attr("target", frameName);					frameableDomains[matched[0]](a[0]);					if(popup.find("iframe[name="+frameName+"]").length===0){						popup.append($(" ").attr("name", frameName).addClass("indexer-reference-frame"));					}				}else{					a.attr("target", "_blank");				}			}); });	}); }

// Only activate on Vector skin if ( mw.config.get( 'skin' ) === 'vector' ) { $(function {			console.log('this happened');			// Make sure we are in article, project, or help namespace			if ( wgCanonicalNamespace === '' || wgCanonicalNamespace === 'Project' || wgCanonicalNamespace === 'Help' || wgCanonicalNamespace === 'Draft' ) {				processPage;			}		}); } })(window, window.console, mediaWiki, jQuery);