User:Enterprisey/revert-and-block.js

// $( function {    var ADVERT = " (RNB)";    var IP_BLOCK_LENGTH = window.revertAndBlockIpBlockLength || "31 hours";    if( mw.config.get( "wgAction" ) === "history" ) {        mw.loader.using( [ "mediawiki.util", "mediawiki.api" ] ).then( function  { var MONTHS = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']; var api = new mw.Api;

function deliverBlockTemplate( username, isAnon ) { var now = new Date; var sectionName = MONTHS[now.getMonth] + " " + now.getFullYear; api.get( {                   prop: "revisions",                    rvprop: "content",                    rvlimit: "1",                    rvslots: "main",                    titles: "User talk:" + username,                    formatversion: "2"                } ).then( function ( data ) {                    var existingText;                    if( data.query.pages[0].missing ) {                        existingText = "";                    } else {                        existingText = data.query.pages[0].revisions[0].slots.main.content;                    }                    var shouldAddSectionHeader = !( new RegExp( /==\s*/.source +                        sectionName.replace( " ", "\\s*" ) + /\s*==/.source ).test( existingText ) );

var textToAdd = "\n\n" + ( shouldAddSectionHeader                           ? "== " + sectionName + " ==\n\n"                            : ""                        ) + "" +                      ( isAnon                            ? "anon=yes";

return api.postWithToken( "csrf", {                       action: "edit",                        title: "User talk:" + username,                        appendtext: textToAdd,                        summary: "You have been blocked from editing for persistent vandalism." + ADVERT                    } ); } ).then( function { mw.notify( "Notification sent." ); } );           }

function appendLink( obj ) { obj.append( $( " " ).append( $( "" ) .attr( "href", "#" ) .text( "RNB" ) .click( function {                        var api = new mw.Api;                        var parentLine = $( this ).parents( "li" );                        var username = parentLine.find( ".history-user a" ).get( 0 ).textContent;                        var parentLineEl = parentLine.get(0);

// A crucial confirmation...                       if( window.revertAndBlockNoConfirm || confirm( "Revert and block " + username + "?" ) ) {

// Revert edit api.postWithToken( "csrf", {                               action: "edit",                                title: mw.config.get( "wgPageName" ),                                undo: parentLineEl.dataset.mwRevid,                                undoafter: parentLineEl.nextElementSibling && parentLineEl.nextElementSibling.dataset.mwRevid                            } ).then( function  { mw.notify( "Undid revision." ); } );

// Add talk page template var isAnon = parentLine.find( ".history-user a" ).hasClass( "mw-anonuserlink" ); deliverBlockTemplate( username, isAnon );

// Place indef/31 hr block (for logged-in users & IPs respectively) api.postWithToken( "csrf", {                               action: "block",                                user: username,                                expiry: isAnon ? IP_BLOCK_LENGTH : "never",                                reason: "Vandalism",                                nocreate: "true",                                autoblock: "true",                                watchuser: "true",                            } ).then( function  { mw.notify( "Blocked." ); } );                       }                        return false; } ) ) );           }            var selector = "span.mw-changeslist-links:not(.mw-history-histlinks):not(.mw-usertoollinks)";

// Adding the hook automatically calls appendLink for the first time mw.hook( "wikipage.content" ).add( function ( obj ) { appendLink( obj.find( selector ) ); } ); } );   } } ); //