MediaWiki:Gadget-Botify.js
Note: After publishing, you may have to bypass your browser's cache to see the changes.
- Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
- Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
- Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
- Opera: Press Ctrl-F5.
// --------------------------------------------------------
// Will Bot the user just before the Replace Text starts
// --------------------------------------------------------
function isBot() { console.log("isBot()");
var isBot;
(new mw.Api()).get({
action: 'query',
meta: 'userinfo',
uiprop: 'groups'
}).done(function(data){
var userGroups = data.query.userinfo.groups;
console.log(userGroups);
isBot = (userGroups.indexOf("bot") > -1);
});
return isBot;
}
function getRightsToken() { console.log("getRightsToken()");
var rightsToken;
(new mw.Api()).get({
action: 'query',
meta: 'tokens',
type: 'userrights'
}).done(function(data){
rightsToken = data.query.tokens.userrightstoken;
console.log("Rights Token: "+rightsToken);
});
return rightsToken;
}
function botify( makeBot, userName, rightsToken, botExpiry ) { console.log("botify()");
var apiOptions = {
action: 'userrights',
user: userName,
//add, expiry
//OR
//remove
token: rightsToken
};
if (makeBot) {
apiOptions.add = 'bot';
apiOptions.expiry = botExpiry; //TODO:Not working: It doesn't expire...
} else {
apiOptions.remove = 'bot';
}
(new mw.Api()).post(
apiOptions
).done(function(data){
console.log(data);
alert( isBot() ? "You are now a bot." : "You are now not a bot." );
} );
}
function botOnReplaceText( rightsToken, userName ) { console.log("botOnReplaceText()");
$('#choose_pages').on('submit', function(e){
//If bot, do nothing
if (isBot()) return;
//If not bot, make the user a bot
botify(true, userName, rightsToken, '1 minute');
});
}
//When document ready:
$(function(){
//Get token TODO: check if token is cached/cookie'd already and that it's still valid?
var rightsToken = getRightsToken(),
userName = mw.config.get("wgUserName");
botOnReplaceText(rightsToken,userName);
//Link in Tools in sidebar to unbot
mediaWiki.util.addPortletLink(
"p-tb",
"#",
"Unbot",
"ca-unbot",
"Click to unbot"
); console.log("Unbot link added");
$("#ca-unbot").on("click", function(e){
e.preventDefault();
botify(false, userName, rightsToken);
}); console.log("Unbot click event added");
});