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 //

const REBOT_BUTTON_ID = "ca-rebot"; const UNBOT_BUTTON_ID = "ca-unbot"; const BANNER_CLASS = "bot-mode-banner";

$(document).ready(function{	botOnReplaceText;	addPortletLinks;	addBotModeBanner;	isBot.then(toggleState); });

/* 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 = '' , 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, function{ $('#choose_pages').off('submit').submit; });		});	} }

function addPortletLinks { //Link in Tools in sidebar to bot mediaWiki.util.addPortletLink("p-tb", "#", "Rebot", REBOT_BUTTON_ID, "Click to rebot"); console.log("Rebot link added"); //Event listener for that link $("#"+REBOT_BUTTON_ID).on("click", function(e){		e.preventDefault;		botify('add');		toggleState(true);	}); console.log("Rebot click event added"); //Link in Tools in sidebar to unbot mediaWiki.util.addPortletLink("p-tb", "#", "Unbot", UNBOT_BUTTON_ID, "Click to unbot"); console.log("Unbot link added"); //Event listener for that link $("#"+UNBOT_BUTTON_ID).on("click", function(e){		e.preventDefault;		botify('remove');		toggleState(false);	}); console.log("Unbot click event added"); }

/* 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 the "bot mode" indication banner, which is hidden by default. function addBotModeBanner { const banner = $(" Bot mode is on. ").addClass(BANNER_CLASS); $("body").prepend(banner); }

function isBot { return new mw.Api.get({		action: "query",		meta: "userinfo",		uiprop: "groups",	}).then(function(result) {		return result.query.userinfo.groups.includes("bot");	}); }

// Determine which portlet link to show, and whether to show the "bot mode" banner function toggleState(isBot) { if (isBot) { $("#"+REBOT_BUTTON_ID).css("display", "none"); $("#"+UNBOT_BUTTON_ID).css("display", ""); $("."+BANNER_CLASS).addClass(BANNER_CLASS+"--enabled"); }	else { $("#"+UNBOT_BUTTON_ID).css("display", "none"); $("#"+REBOT_BUTTON_ID).css("display", ""); $("."+BANNER_CLASS).removeClass(BANNER_CLASS+"--enabled"); } }