MediaWiki:Gadget-Botify.js

// // Will Bot the user just before the Replace Text starts // Notes: https://doc.wikimedia.org/mediawiki-core/master/js/#!/api/mw.Api //

/* Either Bots or Unbots the current user */ function botify( addOrRemove, reason, callback ) { var hasCallback = !!callback; //Create api options object var apiOptions = { action: 'userrights', user: mw.config.get("wgUserName"), };	apiOptions[addOrRemove] = 'bot'; if (reason) apiOptions.reason = reason; //Use POST to change bot userright (new mw.Api).postWithToken('userrights', apiOptions).done(function(data){		console.log(data);		console.log("Botify: '" + addOrRemove + "' success.");		mw.notify("Botify: '" + addOrRemove + "' success.");		if (hasCallback) callback;	}).fail(function{		console.log("Error in botify: '" + addOrRemove + "'.");		mw.notify("Error in botify: '" + addOrRemove + "'.");	}); }

/* Adds an Event Listener for the Replace Text submit button */ function botOnReplaceText { if (  mw.config.get('wgPageName') === 'Special:ReplaceText'	    && document.getElementById('choose_pages')              ) { var reason = ''; $('#choose_pages > p').each(function{ //Though, as far as I know there's only one such p element anyway			var $this = $(this);			if ($this.text.startsWith("Replace")) {				var replaceFrom = $this.find('> code:first-of-type').text,				   replaceTo   = $this.find('> code:last-of-type').text;				reason = 'Text replacement - "' + replaceFrom + '" to "' + replaceTo + '"';				return false; //Break out of the .each			}		}); $('#choose_pages').on('submit', function(e){			e.preventDefault;			botify('add', reason, function{ $('#choose_pages').off('submit').submit; });		});	} }

//When document ready: $(function{	botOnReplaceText;	//Link in Tools in sidebar to bot	mediaWiki.util.addPortletLink("p-tb", "#", "Rebot", "ca-rebot", "Click to rebot");	console.log("Rebot link added");	//Event listener for that link	$("#ca-rebot").on("click", function(e){ e.preventDefault; botify('add'); }); console.log("Rebot click event added");	//Link in Tools in sidebar to unbot	mediaWiki.util.addPortletLink("p-tb", "#", "Unbot", "ca-unbot", "Click to unbot");	console.log("Unbot link added");	//Event listener for that link	$("#ca-unbot").on("click", function(e){ e.preventDefault; botify('remove'); }); console.log("Unbot click event added"); });