MediaWiki:Gadget-Botify.js: Difference between revisions

From Zelda Wiki, the Zelda encyclopedia
Jump to navigation Jump to search
(Now that we're on 1.29, attempting to add expiry functionality to Replace Text auto-botting)
m (Forgot to add console log to debug...)
Line 34: Line 34:


/* Adds an Event Listener for the Replace Text submit button */
/* Adds an Event Listener for the Replace Text submit button */
function botOnReplaceText() {
function botOnReplaceText() {console.log("botOnReplaceText()");
if (  mw.config.get('wgPageName') === 'Special:ReplaceText'
if (  mw.config.get('wgPageName') === 'Special:ReplaceText'

Revision as of 19:39, 16 October 2017

// --------------------------------------------------------
// 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, expiry, 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;
	if (expiry && addOrRemove === 'add') apiOptions.expiry = expiry;
	
	//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() {console.log("botOnReplaceText()");
	
	if (   mw.config.get('wgPageName') === 'Special:ReplaceText'
	    && document.getElementById('choose_pages')              ) {
		
		var reason = ''
		  , replaceFrom = $('#choose_pages > input[name=target]').val()
		  , replaceTo   = $('#choose_pages > input[name=replacement]').val();
		reason = 'Text replacement - "' + replaceFrom + '" to "' + replaceTo + '"';
		
		$('#choose_pages').on('submit', function(e){
			e.preventDefault();
			botify('add', reason, '5 minutes', 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");
	
});