User:Timotheus Canens/massblock.js

//Tim's mass blocking tool

//Adapted from User:Animum/massdelete.js

function doMassBlock() {

document.getElementById("wpMassBlockSubmit").disabled = true;

var users = document.getElementById("wpMassBlockUsers").value.split("\n");

if(users.length == 0) return;

var wpMassBlockReasons = document.getElementById("wpMassBlockReasons").value, wpMassBlockReason = document.getElementById("wpMassBlockReason").value, blocked = 0, talkpageedited = 0, userpageedited = 0, failed = new Array(), error = new Array();

var wpMassBlockAnononly = document.getElementById("wpMassBlockAnononly").checked, wpMassBlockNocreate = document.getElementById("wpMassBlockNocreate").checked, wpMassBlockEmail = document.getElementById("wpMassBlockEmail").checked, wpMassBlockAutoblock = document.getElementById("wpMassBlockAutoblock").checked, wpMassBlockTalkpage = document.getElementById("wpMassBlockTalkpage").checked, wpMassBlockReblock = document.getElementById("wpMassBlockReblock").checked;

var wpMassBlockMessage = document.getElementById("wpMassBlockMessage").value, wpMassBlockTag = document.getElementById("wpMassBlockTag").value, wpMassBlockExpiry = document.getElementById("wpMassBlockExpiry").value;

var wpMassBlockSummaryTalk = document.getElementById("wpMassBlockSummaryTalk").value, wpMassBlockSummaryUser = document.getElementById("wpMassBlockSummaryUser").value, wpMassBlockExpiry = document.getElementById("wpMassBlockExpiry").value;

var blocksettingstring = "&expiry=" + encodeURIComponent(wpMassBlockExpiry == ""? "indefinite" : wpMassBlockExpiry) + (wpMassBlockAnononly ? "&anononly=1" : "") + (wpMassBlockNocreate ? "&nocreate=1" : "") + (wpMassBlockAutoblock ? "&autoblock=1" : "") + (wpMassBlockEmail ? "&noemail=1" : "") + (wpMassBlockTalkpage ? "" : "&allowusertalk=1") + (wpMassBlockReblock ? "&reblock=1" : "");

for(i=0;i

var user = users[i];

if(user.length > 0) {

var req = new XMLHttpRequest();

req.open("GET", mw.config.get('wgScriptPath') + "/api.php?format=json&action=query&prop=info&meta=tokens&type=csrf&titles=User:" + encodeURIComponent(user), false);

req.send(null);

var query = eval("(" + req.responseText + ")").query;

var blocktoken = query.tokens.csrftoken;

var response = query.pages;

for(var index in response) {

var info = response[index];

var postdata = "format=json"

+ "&action=block"

+ "&user=" + encodeURIComponent(user)

+ "&reason=" + encodeURIComponent(wpMassBlockReasons == "other" ? wpMassBlockReason : wpMassBlockReasons + (wpMassBlockReason ? ": " + wpMassBlockReason : ""))

+ blocksettingstring

+ "&token=" + encodeURIComponent(blocktoken);

var req = new XMLHttpRequest();

req.open("POST", mw.config.get('wgScriptPath') + "/api.php", false);

req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

req.setRequestHeader("Content-length", postdata.length);

req.send(postdata);

if(eval("(" + req.responseText + ")")['block']) { //If blockd, update the blockd count and the button.

blocked++;

// now edit user and user talk.

if(wpMassBlockMessage != "") {

var postdata = "format=json&action=edit&watchlist=nochange&title=User%20talk:" + encodeURIComponent(user) + "&summary=" + encodeURIComponent(wpMassBlockSummaryTalk)

+ "&appendtext=" + encodeURIComponent(wpMassBlockMessage)

+ "&token=" + encodeURIComponent(blocktoken);

var req = new XMLHttpRequest();

req.open("POST", mw.config.get('wgScriptPath') + "/api.php", false);

req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

req.setRequestHeader("Content-length", postdata.length);

req.send(postdata);

if(eval("(" + req.responseText + ")")['edit']) { talkpageedited ++; }

else { //If not edited, add the title to the "failed" array and a description of the error to the "error" array.

failed.push("User talk:" + user);

error.push(eval("(" + req.responseText + ")").error.info);

}

}

if(wpMassBlockTag != "") {

var postdata = "format=json&action=edit&watchlist=nochange&title=User:" + encodeURIComponent(user) + "&summary=" + encodeURIComponent(wpMassBlockSummaryUser)

+ "&text=" + encodeURIComponent(wpMassBlockTag)

+ "&token=" + encodeURIComponent(blocktoken);

var req = new XMLHttpRequest();

req.open("POST", mw.config.get('wgScriptPath') + "/api.php", false);

req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

req.setRequestHeader("Content-length", postdata.length);

req.send(postdata);

if(eval("(" + req.responseText + ")")['edit']) { userpageedited ++; }

else { //If not edited, add the title to the "failed" array and a description of the error to the "error" array.

failed.push("User talk:" + user);

error.push(eval("(" + req.responseText + ")").error.info);

}

}

document.getElementById("wpMassBlockSubmit").value = "(" + blocked + "/" + talkpageedited + "/" + userpageedited + ")";

} else { //If not blocked, add the title to the "failed" array and a description of the error to the "error" array.

failed.push("Special:Block/" + user);

error.push(eval("(" + req.responseText + ")").error.info);

}

}

}

if(!users[i+1]) {

document.getElementById("wpMassBlockSubmit").value = "(" + blocked + "/" + talkpageedited + "/" + userpageedited + ")";

if(failed.length > 0) {

var linkedList = "";

for(x=0; x

linkedList += "

  • " + failed[x] + ": " + error[x] + "
  • "; //Links the titles in the "failed" array

    }

    document.getElementById("wpMassBlockFailedContainer").innerHTML += '
    Failed actions:

      ' + linkedList + '
    ';

    }

    }

    }

    }

    function massblockform() {

    var bodyContent;

    switch (mw.config.get('skin')) {

    case 'modern':

    bodyContent = 'mw_contentholder';

    break;

    case 'cologneblue':

    bodyContent = 'article';

    break;

    case 'monobook':

    case 'vector':

    default:

    bodyContent = 'bodyContent';

    break;

    }

    document.getElementsByTagName("h1")[0].textContent = "Tim's mass-blocking tool";

    document.title = "Tim's mass-blocking tool - Wikipedia, the free encyclopedia";

    document.getElementById(bodyContent).innerHTML = '

    From Wikipedia, the free encyclopedia



    '

    + '

    '

    + 'If you abuse this tool, it\'s your fault, not mine.'

    + '

    '

    + '

    '

    + 'Users to block (one on each line, please):
    '

    + ''

    + 'Talk page message, if any (leave blank to leave no message):
    '

    + ''

    + 'Replace user page text with (leave blank for no change):
    '

    + ''

    + '

    '

    + '

    '

    + '

    '

    + '

    '

    + '

    '

    + '

    '

    + '

    '

    + '

    '

    + '

    '

    + '

    '

    + '

    '

    + '

    '

    + '

    '

    + '

    '

    + '

    '

    + '

    '

    + '

    '

    + '

    '

    + '';

    document.getElementById("wpMassBlockReasons").onchange = function() {

    var maxlength = (document.getElementById("wpMassBlockReasons").value == "other" ? 255 : 253-document.getElementById("wpMassBlockReasons").value.length); //It's 25e because of the two characters (": ") in addition to the selected summary.

    document.getElementById("wpMassBlockReason").setAttribute("maxlength", maxlength);

    }

    }

    if(mw.config.get("wgNamespaceNumber") == -1 && mw.config.get("wgTitle").toLowerCase() == "massblock" && /sysop/.test(mw.config.get("wgUserGroups"))) $(massblockform);

    Common reasons:
    Other/additional reason:
    Expiration time (blank for indefinite):
    Edit summary for talk page edit:
    Edit summary for user page edit:
    Block anonymous users only (IPs only):
    Enable autoblock (accounts only):
    Block account creation:
    Block email:
    Remove talk page access:
    Override existing blocks: