User:Eckserah/common.js

(function {	if (window.WhatReallyLinksHereLoaded) return;	window.WhatReallyLinksHereLoaded = true;	mw.loader.using( 'mw.Api' ).then(function { var api = new mw.Api; var actualPageLinks = "No Links Found"; var linkBase = location.protocol + '//' + location.host + '/wiki/'; var whatReallyLinksHerePage = "User:Eckserah/WhatReallyLinksHere"; var processingLinks = false; var numLinks = 0; var lasthash = window.hash; function getLinksFromPage(pageName) { api.get({				action: 'query',				prop: 'linkshere',				format: 'json',				lhshow: '!redirect',				lhlimit: 500,				titles: pageName			}).done(function(e) {				var linkInfos = e.query.pages;		       for (var titleLink in linkInfos) {		        	if (linkInfos[titleLink].linkshere !== undefined) {			            numLinks = linkInfos[titleLink].linkshere.length;			            for (var linkInf in linkInfos[titleLink].linkshere) {			                var linkCheck = checkIfLinksReal(linkInfos[titleLink].linkshere[linkInf],pageName);			            }		        	} else {		        		$('#AllWhatLinksHere').append(actualPageLinks);		        		$('#LoadingInfo').hide;		        	}		        }			}); }		function checkIfLinksReal(pageLinks, basePageName) { api.get({				action: 'parse',				prop: 'wikitext',				format: 'json',				page: pageLinks.title			}).done(function(data) {				if (data.error === undefined) {		           var wikitext = data.parse.wikitext['*'];		            if (findLinkInPage(basePageName, wikitext)) {		                $('#AllWhatLinksHere').append($(' '+pageLinks.title+' '));		                var allLinks = $('#AllWhatLinksHere div').sort(function (a,b) { var aText = $(a).find('a').text; var bText = $(b).find('a').text; if (aText > bText) return 1; if (aText < bText) return -1; return 0; });						$('#AllWhatLinksHere').append(allLinks);		           }		            numLinks = numLinks - 1;		            if (numLinks === 0) {		            	if ($('#AllWhatLinksHere').find('div').length === 0)		            		$('#AllWhatLinksHere').append(actualPageLinks);		                $('#LoadingInfo').hide;		                processingLinks = false;		            }		        } else {		        	numLinks = numLinks - 1;		        	console.log(parseWikiTextUrl+pageLinks.title);		        	console.log(data.error);		        }			}); }		function concatPageLink { return linkBase+whatReallyLinksHerePage+'#'+location.pathname.replace('/wiki/',''); }		function escapeRegExp(text) { return text.replace(/[-[\]{}*+?.,\\^$|#\s]/g, '\\$&'); }		function findLinkInPage(SearchPageTitle, wikiText) { var modPageTitle = escapeRegExp(SearchPageTitle).replace(/[ _]/g,'[ _]'); //var regexStr = '\\[\\[' + modPageTitle + '*[^\\]]+\\]\\]'; var regexStr = '\\[{2}' + modPageTitle + '[|#\]*.*\\]{2}'; var testReg = new RegExp(regexStr); return testReg.test(wikiText); }		$(document).ready(function {			var IsBetterWhatLinksHere = $('#BetterWhatLinksHere');			if (IsBetterWhatLinksHere.length > 0) {				$('#BetterWhatLinksHereForm').submit(function(e) { e.preventDefault; if (processingLinks) return; processingLinks = true; $('#LoadingInfo').show; $('#AllWhatLinksHere').html(''); var searchText = $('#BetterWhatLinksHereForm').find('input[type=text]').val; if (searchText !== '') { setTimeout(function {							getLinksFromPage(decodeURIComponent(searchText));						}, 200); }					return false; });				if (location.hash !== '') {					$('#BetterWhatLinksHereForm').find('input[type=text]').val(location.hash.substring(1));					$('#BetterWhatLinksHereForm').submit;				}				window.onhashchange = function {					if (location.hash != lasthash) {						lasthash = location.hash;						$('#BetterWhatLinksHereForm').find('input[type=text]').val(location.hash.substring(1));						$('#BetterWhatLinksHereForm').submit;					}				};			}			var whatReallyLinksHereElem = $(' ').append($('').text('What really links here'));			var baseLink = $('#WikiaBarWrapper').find('[data-name=whatlinkshere]');			if (baseLink.length > 0) {				whatReallyLinksHereElem.find('a').prop('href',baseLink.prop('href').replace('Special:WhatLinksHere/',whatReallyLinksHerePage+'#'));				baseLink = baseLink.parent;				whatReallyLinksHereElem.insertAfter(baseLink);			} else {				baseLink = $('#WikiaBarWrapper').find('.tools-customize'); whatReallyLinksHereElem.find('a').prop('href',concatPageLink); whatReallyLinksHereElem.insertBefore(baseLink); }		});	}); });