User:Joeytje50/shortLink.js

$(window).on('load',function {	var accesskey = !$('[accesskey="s"]').length;	$('#t-shortlink').remove; //prevent duplication	$('#t-whatlinkshere').after('Get short URL'); });

window._shortURL = {}; _shortURL.shorten = function(page) { var slash = page.charAt(0) == '?' ? '' : '/';	var lang = document.location.host.split('.')[0]; var shortpage = page.replace(/^Project\:/,'WP:'); //Not all wikis support WP:->Project: redirection, so this is not default. switch (lang) { case 'en': return 'http://enwp.org'+slash+shortpage; case 'fr': return 'http://frwp.org'+slash+shortpage; default: if (slash) {return 'http://enwp.org/'+lang+page;} else {return 'http://'+document.location.host+'/wiki/'+page;} } };

_shortURL.getShortenedURL = function(cont) { if (_shortURL.list && !cont) return _shortURL.addShortenedURL; //Apparently the shortened urls have been looked for before. _shortURL.list = _shortURL.list||[]; $.get('https://en.wikipedia.org/w/api.php?action=query&list=backlinks&blfilterredir=redirects&bllimit=500&format=json&bltitle='+mw.util.wikiUrlencode(mw.config.get("wgPageName"))+(cont||''), function(response) {		var list = response.query.backlinks;		list.push({"pageid":mw.config.get("wgArticleId"), "ns":mw.config.get("wgNamespaceNumber"), "title":(mw.config.get("wgCanonicalNamespace")&&mw.config.get("wgCanonicalNamespace")+':')+mw.config.get("wgTitle"), "redirect":""});		for (var i=0;i<list.length;i++) {			var urls = [_shortURL.shorten(mw.util.wikiUrlencode(list[i].title)), {0:_shortURL.shorten('?curid='+list[i].pageid), 1:list[i].title}];			urls[1].length = urls[1][0].length;			for (var n=0;n<urls.length;n++) {				if (!_shortURL.list[urls[n].length]) _shortURL.list[urls[n].length] = [urls[n]];				else _shortURL.list[urls[n].length].push(urls[n]);			}		}		if (response['query-continue']) { var blcont = '&blcontinue='+response['query-continue'].backlinks.blcontinue; _shortURL.getShortenedURL(blcont); } else { _shortURL.addShortenedURL; }	}); };

_shortURL.addShortenedURL = function { var $blackout = $(' ').appendTo('body'); $blackout.click(_shortURL.closeShortURLPopup); var $popupBox = $(' ').appendTo('body'); $popupBox.prepend(' Left-click on the URL you like to select, and then press CTRL+C to copy it, or right click the URL and choose the option to copy the link address. \						You can also open the link by right clicking it and choosing one of the open link options, by middle clicking (via the scroll wheel), by holding the Ctrl or Shift key and clicking it, or by dragging the link to your current url bar. '); $popupBox.append('X'); $popupBox.append('?'); for (var i=0,shortest=0;i<_shortURL.list.length&&(shortest>i-5||!shortest||$popupBox.children('.shortURLLink').length<10);i++) { if (_shortURL.list[i]) { shortest = shortest||i; $popupBox.append(' Length '+i+' '); for (var n=0,url,target='';n<_shortURL.list[i].length;n++) { url = _shortURL.list[i][n]; if (typeof url == 'object') { target = ' => '+url[1]+''; url = url[0]; }				$popupBox.append('<li><a href="'+url+'" class="shortURLLink" style="outline:none;">'+url+target+'</a></li>'); }		}	}	$popupBox.click(function(e) {		var $target = $(e.target).is('a.shortURLLink *') ? $(e.target).parent : $(e.target);		if ($('#shortURLSelected').length && !$target.is('#shortURLSelected')) $('#shortURLSelected').prev.show.next.remove;		if (!$target.is('a.shortURLLink, a.shortURLLink *') || e.shiftKey || e.ctrlKey || e.which == 2) return;		e.preventDefault;		$target.hide;		$('<input type="text" id="shortURLSelected" style="width:75%;min-width:300px;" value="'+$target[0].href+'"/>').insertAfter($target).select;	}); };

_shortURL.closeShortURLPopup = function { $('#shortURLBlackout, #shortURLPopup').remove; };