Jump to content

User:Enterprisey/massblock.js

From Wikipedia, the free encyclopedia
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
//<nowiki>
if( mw.config.get( "wgPageName" ) === "Special:BlankPage/mass-block" ) {
    $( "#mw-content-text" ).empty()
        .append( "Usernames (one per line):" )
        .append( $( "<textarea>", { "rows":"8", "cols":"100", "id":"mass-block-users" } ) )
        .append( "<br />" )
        .append( "Reason (will also be used for edit summary on talk page): " )
        .append( $( "<input>", { "type":"text", "length":"256", "id":"mass-block-reason" } ) )
        .append( "<br />" )
        .append( "Talk page section name: " )
        .append( $( "<input>", { "type":"text", "length":"256", "id":"mass-block-talk-header" } ) )
        .append( "<br />" )
        .append( "Talk page message: " )
        .append( $( "<textarea>", { "rows":"8", "cols":"100", "id":"mass-block-talk" } ) )
        .append( "<br />" )
        .append( $( "<button>" ).text( "Block all" ).click( function () {
            mw.loader.using( [ "mediawiki.api" ], function () {
                var api = new mw.Api();
                var reason = $( "#mass-block-reason" ).val();
                var users = $( "#mass-block-users" ).val().split( '\n' );
                var talkHeader = $( "#mass-block-talk-header" ).val();
                var talkMessage = $( "#mass-block-talk" ).val();
                users.forEach( function ( username ) {
                    api.postWithToken( "csrf", {
                        action: "block",
                        user: username,
                        expiry: "indefinite",
                        reason: reason,
                        anononly: true, // just because twinkle has it on
                        nocreate: false,
                        autoblock: false,
                        noemail: false,
                        allowusertalk: true
                    } ).then( function () {
                        console.log("Blocked " + username);
                        return api.postWithToken( "csrf", {
                            action: "edit",
                            title: "User talk:" + username,
                            section: "new",
                            sectiontitle: talkHeader,
                            text: talkMessage,
                            summary: reason,
                        } ).then( function(){console.log("Notified " + username);} )
                            .catch( function ( e ) { console.error( e ); } );
                    }.bind( this ) ).catch( function ( e ) { console.error( e ); } );
                } ) // end forEach callback
            } ); // end mw.loader.using
        } ) ); // end button click handler
}
// </nowiki>