MediaWiki:Gadget-Botify.js: Difference between revisions

From Zelda Wiki, the Zelda encyclopedia
Jump to navigation Jump to search
m (Rewrote; ajax calls have to use done/fail/always; used more mw.Api methods; untested)
(add emoji for pizzaz?)
 
(20 intermediate revisions by 3 users not shown)
Line 3: Line 3:
// Notes: https://doc.wikimedia.org/mediawiki-core/master/js/#!/api/mw.Api
// Notes: https://doc.wikimedia.org/mediawiki-core/master/js/#!/api/mw.Api
// --------------------------------------------------------
// --------------------------------------------------------
const REBOT_BUTTON_ID = "t-botify";
const UNBOT_BUTTON_ID = "t-unbot";
const BANNER_CLASS = "bot-mode-banner";
$(document).ready(function(){
botOnReplaceText();
addToolboxLinks();
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 addToolboxLinks() {
addToolboxItem({
id: REBOT_BUTTON_ID,
text: 'Bot mode 🤖',
title: 'Click to add yourself to the bot group',
onClick: function() {
botify('add');
toggleState(true);
}
});
addToolboxItem({
id: UNBOT_BUTTON_ID,
text: 'Unbot',
title: 'Click to unbot',
onClick: function() {
botify('remove');
toggleState(false);
}
});
}
function addToolboxItem(item) {
var toolbox = document.getElementById('p-tb-list');
var listItem = $('<li>', { id: item.id });
var it = $('<a>' + item.text + '</a>', { title: item.title})
.click(function(e) {
e.preventDefault();
    item.onClick();
})
.wrap(listItem)
.parent()
.appendTo(toolbox);
}


/* Either Bots or Unbots the current user */
/* Either Bots or Unbots the current user */
function botify( addOrRemove, expiry, callback ) {
function botify( addOrRemove, reason, callback ) {
var hasCallback = !!callback;
//Create api options object
var apiOptions = {
var apiOptions = {
action: 'userrights',
action: 'userrights',
Line 12: Line 79:
};
};
apiOptions[addOrRemove] = 'bot';
apiOptions[addOrRemove] = 'bot';
if (addOrRemove === 'add') apiOptions.expiry = '1 minute' ; //TODO:Not working: It doesn't expire...
if (reason) apiOptions.reason = reason;
var api = new mw.Api();
//Use POST to change bot userright
api.postWithToken('userrights', apiOptions).done(function(data){
(new mw.Api()).postWithToken('userrights', apiOptions).done(function(data){
console.log(data);
console.log(data);
console.log("User right changed");
console.log("Botify: '" + addOrRemove + "' success.");
mw.notify("Botify: '" + addOrRemove + "' success.");
//TODO://Temporary for testing, don't forget to move the response() out
if (hasCallback) callback();
api.getUserInfo().done(function(data){
console.log(data.groups);
alert( (data.groups.indexOf("bot") > -1) ? "You are now a bot." : "You are now not a bot." );
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 */
// Adds the "bot mode" indication banner, which is hidden by default.
function botOnReplaceText() { console.log("botOnReplaceText()");
function addBotModeBanner() {
const banner = $("<div>Bot mode is on.</div>").addClass(BANNER_CLASS);
$('#choose_pages').on('submit', function(e){
$("body").prepend(banner);
e.preventDefault();
}
(new mw.Api()).getUserInfo().done(function(data){
 
console.log(data.groups);
function isBot() {
if (data.groups.indexOf("bot") === -1) botify('add', '1 minute', function(){$('#choose_pages').off('submit').submit()});
return new mw.Api().get({
});
action: "query",
meta: "userinfo",
uiprop: "groups",
}).then(function(result) {
return result.query.userinfo.groups.includes("bot");
});
});
}
}


//When document ready:
// Determine which portlet link to show, and whether to show the "bot mode" banner
$(function(){
function toggleState(isBot) {
if (isBot) {
botOnReplaceText();
$("#"+REBOT_BUTTON_ID).css("display", "none");
$("#"+UNBOT_BUTTON_ID).css("display", "");
//Link in Tools in sidebar to unbot
$("."+BANNER_CLASS).addClass(BANNER_CLASS+"--enabled");
mediaWiki.util.addPortletLink("p-tb", "#", "Unbot", "ca-unbot", "Click to unbot");
}
console.log("Unbot link added");
else {
//Event listener for that link
$("#"+UNBOT_BUTTON_ID).css("display", "none");
$("#ca-unbot").on("click", function(e){
$("#"+REBOT_BUTTON_ID).css("display", "");
e.preventDefault();
$("."+BANNER_CLASS).removeClass(BANNER_CLASS+"--enabled");
botify('remove');
}
}); console.log("Unbot click event added");
}
});

Latest revision as of 01:22, 8 November 2020

// --------------------------------------------------------
// 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 = "t-botify";
const UNBOT_BUTTON_ID = "t-unbot";
const BANNER_CLASS = "bot-mode-banner";

$(document).ready(function(){
	botOnReplaceText();
	addToolboxLinks();
	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 addToolboxLinks() {
	addToolboxItem({
		id: REBOT_BUTTON_ID,
		text: 'Bot mode 🤖',
		title: 'Click to add yourself to the bot group',
		onClick: function() {
			botify('add');
			toggleState(true);
		}
	});
	addToolboxItem({
		id: UNBOT_BUTTON_ID,
		text: 'Unbot',
		title: 'Click to unbot',
		onClick: function() {
			botify('remove');
			toggleState(false);
		}
	});
}

function addToolboxItem(item) {
	var toolbox = document.getElementById('p-tb-list');
	var listItem = $('<li>', { id: item.id });
	var it = $('<a>' + item.text + '</a>', { title: item.title})
		.click(function(e) {
			e.preventDefault();
	    	item.onClick();
		})
		.wrap(listItem)
		.parent()
		.appendTo(toolbox);
}

/* 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 = $("<div>Bot mode is on.</div>").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");
	}
}