MediaWiki:Gadget-Botify.js: Difference between revisions
Jump to navigation
Jump to search
KokoroSenshi (talk | contribs) (Moved from https://zelda.gamepedia.com/index.php?title=User%3AKokoroSenshi%2Fcommon.js&type=revision&diff=567457&oldid=566808) |
KokoroSenshi (talk | contribs) m (Rewrote, untested) |
||
Line 3: | Line 3: | ||
// -------------------------------------------------------- | // -------------------------------------------------------- | ||
function botOnReplaceText() { | function 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() { | |||
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 ) { | |||
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 ) { | |||
$('#choose_pages').on('submit', function(e){ | $('#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( | mediaWiki.util.addPortletLink( | ||
"p-tb", | "p-tb", | ||
"#", | |||
"Unbot", | "Unbot", | ||
"ca-unbot", | "ca-unbot", | ||
"Click to unbot" | "Click to unbot" | ||
); console.log(" | ); console.log("Unbot link added"); | ||
$("#ca-unbot").on("click", function(e) { | |||
$("#ca-unbot").on("click", function(e){ | |||
e.preventDefault(); | e.preventDefault(); | ||
botify(false, userName, rightsToken); | |||
}); console.log("Unbot click event added"); | |||
}); | |||
Revision as of 14:13, 28 June 2017
// --------------------------------------------------------
// Will Bot the user just before the Replace Text starts
// --------------------------------------------------------
function 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() {
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 ) {
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 ) {
$('#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");
});