Jump to content

User:CenPop/common.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
$(window).on('load',function() {
	var accesskey = !$('[accesskey="s"]').length;
	$('#t-shortlink').remove(); //prevent duplication
	$('#p-tb-list #t-whatlinkshere').after('<li id="t-shortlink"><a href="javascript:_shortURL.getShortenedURL();" title="Get a list of the shortest urls you could link to, using the enwp.org url shortener and internal redirects'+(accesskey&&' ['+mw.util.tooltipAccessKeyPrefix+'s]" accesskey="s')+'">Get short URL</a></li>')
});

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":wgArticleId, "ns":wgNamespaceNumber, "title":(wgCanonicalNamespace&&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 = $('<div id="shortURLBlackout" style="position:fixed;top:0;left:0;z-index:99999;height:100%;width:100%;background:black;opacity:0.6;"></div>').appendTo('body');
	$blackout.click(_shortURL.closeShortURLPopup);
	var $popupBox = $('<div id="shortURLPopup" style="position:fixed;top:25%;left:25%;z-index:99999;width:50%;height:50%;border:3px solid black;background:white;font-size:13px;padding:10px;padding-top:40px;overflow:auto;"><ul></ul></div>').appendTo('body');
	$popupBox.prepend('<div style="padding-right:10px;display:none;" id="shortURLPopupInfo">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.<br/>\
						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.</div>')
	$popupBox.append('<a href="javascript:_shortURL.closeShortURLPopup()" style="position:absolute;top:5px;right:5px;font-size:30px;color:black;text-decoration:none;font-family:calibri;">X</a>')
	$popupBox.append('<a href="javascript:$(\'#shortURLPopupInfo\').toggle();" style="position:absolute;top:5px;left:5px;font-size:30px;color:black;text-decoration:none;border-bottom:1px dotted black;line-height:1em;font-family:calibri;">?</a>')
	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('<li><h2>Length '+i+'</h2></li>');
			for (var n=0,url,target='';n<_shortURL.list[i].length;n++) {
				url = _shortURL.list[i][n];
				if (typeof url == 'object') {
					target = ' <i style="float:right;">=> '+url[1]+'</i>';
					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();
}
 
if (mw.config.get('wgCanonicalNamespace')+':'+mw.config.get('wgTitle') === 'Project:CenPop/Script')
	mw.loader.load('//en.wikipedia.org/w/index.php?title=User:CenPop/CenPop.js&action=raw&ctype=text/javascript');