MediaWiki:UsernameAvailability/code.js

//UsernameAvailability - Count of Howard //18-12-16 - Initial revision //Cache mw variables const mwVariables = mw.config.get([		'wgServer',		'wgArticlePath',		'wgPageName',		'wgArticleId',		'wgUserLanguage'	]);
 * (function($, mw) {

const i18n = { 'en': { //English itemTitle: 'Find a username', modalTitle: 'Check Username Availability', modalCancel: 'Cancel', modalSearch: 'Search', modalExists: 'This username exists already!', modalUnclaimed: 'This username is available!', modalView: 'View page', modalError: 'Error: Special characters are forbidden.' },		'sv': { //Swedish itemTitle: 'Hitta användarnamn', modalTitle: 'Kolla Användarnamn Tillgänglighet', modalCancel: 'Avboka', modalSearch: 'Söka', modalExists: 'Detta användarnamn existerar redan!', modalUnclaimed: 'Detta användarnamn är tillgängligt!', modalView: 'Visa sida', modalError: 'Specialtecken är förbjudna.' }	};

//Language stuff const lang = mw.config.get('wgUserLanguage'); const textContent = i18n.hasOwnProperty(lang) ? i18n[lang] : i18n.en;

//Tools menu items const UAItem = document.createElement('li'); const UAItemLink = document.createElement('a');

//Input constants, for security concerns const chars = /^[a-zA-Z0-9- ]*$/; const maxchars = 30;

var modalHTML = '\  \ \				 ' + textContent.itemTitle + ' \ \					 \ \			 \		 ';

//Assemble toolbox elements UAItem.setAttribute('class', 'overflow'); UAItem.setAttribute('id', 'ua-item'); UAItemLink.setAttribute('href', '#'); UAItemLink.textContent = textContent.itemTitle;

//Show/hide handler $.each(['show', 'hide'], function (i, ev) {		var el = $.fn[ev];		$.fn[ev] = function {			this.trigger(ev);			el.apply(this, arguments);			return el;		};	});

//Main $('#my-tools-menu').on('show', function {		$(UAItem).prependTo('#my-tools-menu');		$(UAItemLink).appendTo('#ua-item');		$('#ua-item').click(function { //Prevent multiple modal loads if (document.contains(document.getElementById('ua-modal-window'))) { document.getElementById('ua-modal-window').remove; }			$.showCustomModal(textContent.modalTitle, modalHTML, {				id: 'ua-modal-window',				width: 500,				buttons: [{					message: textContent.modalCancel,					handler: function {						$('#ua-modal-window').closeModal;					}				}, {					message: textContent.modalSearch,					handler: function {						var returnedData, returnedName;						var notice = document.createElement('div');						notice.setAttribute('id', 'ua-notice');

//Make it easier to quickly search multiple usernames $('#ua-input-value').focus(function {							document.getElementById("ua-modal-form").reset;							$(notice).fadeOut("slow", function { notice.remove; });						});

//Basic character checking var inputData = document.getElementById('ua-input-value').value; if(!chars.test(inputData)) { $(notice).html(' ' + textContent.modalError + ' '); $(notice).appendTo('#ua-modal-form'); document.getElementById("ua-modal-form").reset; return; }						var encodedInput = mw.util.wikiUrlencode(inputData);

//URL parameters var pararray = []; var parameters = { action: 'query', list: 'users', ususers: encodedInput, format: 'json' };

//Assembling the API url for (var i in parameters) { pararray.push(mw.util.wikiUrlencode(i) + '=' + mw.util.wikiUrlencode(parameters[i])); }						pararray = '?' + pararray.join('&'); var apiURL = mwVariables.wgServer + mw.util.wikiScript('api') + pararray;

//Prevent multiple notice posts if (document.contains(document.getElementById('ua-notice'))) { document.getElementById('ua-notice').remove; }

//Non-HTML5 solution for placeholder $('[placeholder]').focus(function {							var input = $(this);							if (input.val == input.attr('placeholder')) {								input.val();								input.removeClass('placeholder');							}						}).blur(function {							var input = $(this);							if (input.val ===  || input.val == input.attr('placeholder')) {								input.addClass('placeholder');								input.val(input.attr('placeholder'));							}						}).blur;

//API call $.get(apiURL, function(data){							returnedData = data.query.users[0];							returnedName = data.query.users[0].name;						}).done(function(data) {							var returnedDataUserNameURL = wgArticlePath.replace('$1', 'User:' + mw.util.wikiUrlencode(returnedName));							//A bit janked because of the way the API reports nonexistent usernames							if (returnedData.hasOwnProperty('userid') && typeof returnedData.missing == 'undefined') {								$(notice).html(textContent.modalExists + ' (' + textContent.modalView + ')');							} else {								$(notice).html(textContent.modalUnclaimed + '  (' + textContent.modalView + ')');							}							$(notice).appendTo('#ua-modal-form');						}); }				}]			});		});	}); })(this.jQuery, this.mediaWiki);