User:Euphoria/massBlock.js: Difference between revisions

From Test Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 1: Line 1:
// Check if the current user has admin rights
if (mw.config.get('wgUserGroups').includes('sysop')) {
if (mw.config.get('wgUserGroups').includes('sysop')) {
     // Function to create the blocking interface
     // Function to create the blocking interface
     function createBlockingInterface() {
     function createBlockingInterface() {
         var formHtml = '<div id="massBlockForm" style="background: white; border: 1px solid black; padding: 10px;">'
         // Ensure the interface isn't already open
            + '<h3>Mass Block</h3>'
        if ($('#massBlockTool').length === 0) {
            + 'Usernames (comma-separated):<br><textarea id="usernamesToBlock" rows="2" cols="20"></textarea><br>'
            var formHtml = '<div id="massBlockTool" style="margin: 20px; background: white; border: 1px solid black; padding: 10px;">' +
            + 'Block reason:<br><input type="text" id="blockReason"><br>'
                '<h1>Mass Block Tool</h1>' +
            + 'Block duration:<br><select id="blockDuration">'
                '<p>If you abuse this tool, it\'s your fault, not mine.</p>' +
            + '<option value="6 hours">6 hours</option>'
                '<textarea id="usernamesToBlock" placeholder="Usernames to block (one on each line, please):" rows="10" cols="50"></textarea>' +
            + '<option value="24 hours">24 hours</option>'
                '<h3>Common reasons:</h3>' +
            + '<option value="31 hours">31 hours</option>'
                '<select id="blockReasonSelect">' +
            + '<option value="1 week">1 week</option>'
                '<option value="vandalism">Vandalism</option>' +
            + '<option value="2 weeks">2 weeks</option>'
                '<option value="spam">Spam</option>' +
            + '<option value="1 month">1 month</option>'
                '<option value="harassment">Harassment</option>' +
            + '<option value="3 months">3 months</option>'
                '<option value="other">Other reason</option>' +
            + '<option value="6 months">6 months</option>'
                '</select><br>' +
            + '<option value="1 year">1 year</option>'
                '<input type="text" id="otherBlockReason" placeholder="Other/additional reason" style="display:none; margin-top: 10px;">' +
            + '<option value="5 years">5 years</option>'
                '<h3>Block duration:</h3>' +
            + '<option value="indefinite">indefinite</option>'
                '<select id="blockDuration">' +
            + '</select><br>'
                '<option value="indefinite">indefinite</option>' +
            + '<button id="executeMassBlock">Block Users</button>'
                '<option value="3 hours">3 hours</option>' +
            + '</div>';
                '<option value="12 hours">12 hours</option>' +
                '<option value="24 hours">24 hours</option>' +
                '<option value="3 days">3 days</option>' +
                '<option value="1 week">1 week</option>' +
                '<option value="1 month">1 month</option>' +
                '<option value="3 months">3 months</option>' +
                '<option value="6 months">6 months</option>' +
                '<option value="1 year">1 year</option>' +
                '<option value="other">Other...</option>' +
                '</select><br>' +
                '<input type="text" id="customBlockDuration" placeholder="Custom duration" style="display:none; margin-top: 10px;">' +
                '<h3>Additional options:</h3>' +
                '<input type="checkbox" id="nocreateCheckbox" checked> Prevent account creation<br>' +
                '<input type="checkbox" id="noemailCheckbox" checked> Prevent user from sending email<br>' +
                '<button id="executeMassBlock" style="margin-top: 10px;">Block Users</button>' +
                '</div>';


        // Check if the form already exists to avoid duplicates
        if ($('#massBlockForm').length === 0) {
             $('body').append(formHtml);
             $('body').append(formHtml);
        }


        $('#executeMassBlock').click(function() {
            // Show or hide the custom reason input based on the selected reason
            var users = $('#usernamesToBlock').val().split(',');
            $('#blockReasonSelect').change(function() {
            var reason = $('#blockReason').val();
                if ($(this).val() === 'other') {
            var duration = $('#blockDuration').val();
                    $('#otherBlockReason').show();
            users.forEach(function(user) {
                } else {
                blockUser(user.trim(), reason, duration);
                    $('#otherBlockReason').hide();
                }
            });
 
            // Show or hide the custom duration input based on the selected duration
            $('#blockDuration').change(function() {
                if ($(this).val() === 'other') {
                    $('#customBlockDuration').show();
                } else {
                    $('#customBlockDuration').hide();
                }
            });
 
            // Handle the block users button click
            $('#executeMassBlock').click(function() {
                var usernames = $('#usernamesToBlock').val().split('\n');
                var blockReason = $('#blockReasonSelect').val() === 'other' ?
                    $('#otherBlockReason').val() : $('#blockReasonSelect').find('option:selected').text();
                var duration = $('#blockDuration').val() === 'other' ?
                    $('#customBlockDuration').val() : $('#blockDuration').val();
                var nocreate = $('#nocreateCheckbox').is(':checked');
                var noemail = $('#noemailCheckbox').is(':checked');
 
                usernames.forEach(function(username) {
                    if (username.trim() !== '') {
                        blockUser(username.trim(), blockReason, duration, nocreate, noemail);
                    }
                });
             });
             });
         });
         }
     }
     }


     // Function to block a user
     // Function to block a user
     function blockUser(username, reason, duration) {
     function blockUser(username, reason, duration, nocreate, noemail) {
         var api = new mw.Api();
         var api = new mw.Api();
         api.postWithToken('csrf', {
         api.postWithToken('csrf', {
Line 45: Line 86:
             expiry: duration,
             expiry: duration,
             reason: reason,
             reason: reason,
             nocreate: true,
             nocreate: nocreate ? 1 : 0,
             autoblock: true,
             autoblock: true,
             noemail: true
             noemail: noemail ? 1 : 0
         }).done(function(data) {
         }).done(function(data) {
             console.log('User blocked: ' + username);
             console.log('User blocked: ' + username);
Line 58: Line 99:
     $(document).ready(function() {
     $(document).ready(function() {
         var portletLink = mw.util.addPortletLink(
         var portletLink = mw.util.addPortletLink(
             'p-tb', // This is the ID of the toolbox section in the sidebar
             'p-tb',
             '#', // We'll bind the click event to open the interface
             '#',
             'Mass Block', // The text of the link
             'Mass Block',
             't-massblock', // The ID of the new link
             't-massblock',
             'Mass block users' // Tooltip text for the link
             'Mass block users'
         );
         );


         // Bind click event to the link
         // Bind the click event to the link
         $(portletLink).click(function(e) {
         $(portletLink).click(function(e) {
             e.preventDefault(); // Prevent the default action
             e.preventDefault();
             createBlockingInterface(); // Call the function to create/open the blocking interface
             createBlockingInterface();
         });
         });
     });
     });
}
}

Revision as of 12:03, 28 December 2023

// Check if the current user has admin rights
if (mw.config.get('wgUserGroups').includes('sysop')) {

    // Function to create the blocking interface
    function createBlockingInterface() {
        // Ensure the interface isn't already open
        if ($('#massBlockTool').length === 0) {
            var formHtml = '<div id="massBlockTool" style="margin: 20px; background: white; border: 1px solid black; padding: 10px;">' +
                '<h1>Mass Block Tool</h1>' +
                '<p>If you abuse this tool, it\'s your fault, not mine.</p>' +
                '<textarea id="usernamesToBlock" placeholder="Usernames to block (one on each line, please):" rows="10" cols="50"></textarea>' +
                '<h3>Common reasons:</h3>' +
                '<select id="blockReasonSelect">' +
                '<option value="vandalism">Vandalism</option>' +
                '<option value="spam">Spam</option>' +
                '<option value="harassment">Harassment</option>' +
                '<option value="other">Other reason</option>' +
                '</select><br>' +
                '<input type="text" id="otherBlockReason" placeholder="Other/additional reason" style="display:none; margin-top: 10px;">' +
                '<h3>Block duration:</h3>' +
                '<select id="blockDuration">' +
                '<option value="indefinite">indefinite</option>' +
                '<option value="3 hours">3 hours</option>' +
                '<option value="12 hours">12 hours</option>' +
                '<option value="24 hours">24 hours</option>' +
                '<option value="3 days">3 days</option>' +
                '<option value="1 week">1 week</option>' +
                '<option value="1 month">1 month</option>' +
                '<option value="3 months">3 months</option>' +
                '<option value="6 months">6 months</option>' +
                '<option value="1 year">1 year</option>' +
                '<option value="other">Other...</option>' +
                '</select><br>' +
                '<input type="text" id="customBlockDuration" placeholder="Custom duration" style="display:none; margin-top: 10px;">' +
                '<h3>Additional options:</h3>' +
                '<input type="checkbox" id="nocreateCheckbox" checked> Prevent account creation<br>' +
                '<input type="checkbox" id="noemailCheckbox" checked> Prevent user from sending email<br>' +
                '<button id="executeMassBlock" style="margin-top: 10px;">Block Users</button>' +
                '</div>';

            $('body').append(formHtml);

            // Show or hide the custom reason input based on the selected reason
            $('#blockReasonSelect').change(function() {
                if ($(this).val() === 'other') {
                    $('#otherBlockReason').show();
                } else {
                    $('#otherBlockReason').hide();
                }
            });

            // Show or hide the custom duration input based on the selected duration
            $('#blockDuration').change(function() {
                if ($(this).val() === 'other') {
                    $('#customBlockDuration').show();
                } else {
                    $('#customBlockDuration').hide();
                }
            });

            // Handle the block users button click
            $('#executeMassBlock').click(function() {
                var usernames = $('#usernamesToBlock').val().split('\n');
                var blockReason = $('#blockReasonSelect').val() === 'other' ?
                    $('#otherBlockReason').val() : $('#blockReasonSelect').find('option:selected').text();
                var duration = $('#blockDuration').val() === 'other' ?
                    $('#customBlockDuration').val() : $('#blockDuration').val();
                var nocreate = $('#nocreateCheckbox').is(':checked');
                var noemail = $('#noemailCheckbox').is(':checked');

                usernames.forEach(function(username) {
                    if (username.trim() !== '') {
                        blockUser(username.trim(), blockReason, duration, nocreate, noemail);
                    }
                });
            });
        }
    }

    // Function to block a user
    function blockUser(username, reason, duration, nocreate, noemail) {
        var api = new mw.Api();
        api.postWithToken('csrf', {
            action: 'block',
            user: username,
            expiry: duration,
            reason: reason,
            nocreate: nocreate ? 1 : 0,
            autoblock: true,
            noemail: noemail ? 1 : 0
        }).done(function(data) {
            console.log('User blocked: ' + username);
        }).fail(function(error) {
            console.log('Error blocking user: ' + username);
        });
    }

    // Add link to the sidebar
    $(document).ready(function() {
        var portletLink = mw.util.addPortletLink(
            'p-tb',
            '#',
            'Mass Block',
            't-massblock',
            'Mass block users'
        );

        // Bind the click event to the link
        $(portletLink).click(function(e) {
            e.preventDefault();
            createBlockingInterface();
        });
    });
}