User:Euphoria/massBlock.js

From Test Wiki
Revision as of 12:19, 28 December 2023 by Euphoria (talk | contribs)

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
if (mw.config.get('wgUserGroups').includes('sysop')) {
    // Function to create the blocking interface as if it's a "Special Page"
    function createBlockingInterface() {
        var contentText = $('#mw-content-text');
        contentText.empty(); // Clear the current content

        // Change the document title
        document.title = 'Mass Block Tool - ' + mw.config.get('wgSiteName');

        // Update the header of the content area
        contentText.append('<h1>Mass Block Tool</h1>');

        var formHtml = '<div id="massBlockTool">' +
            '<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>' +
            '<input type="checkbox" id="allowusertalkCheckbox" checked> Allow user to edit their talk page<br>' +
            '<button id="executeMassBlock" style="margin-top: 10px;">Block Users</button>' +
            '</div>';

        contentText.append(formHtml);

        $('#blockReasonSelect').change(function() {
            if ($(this).val() === 'other') {
                $('#otherBlockReason').show();
            } else {
                $('#otherBlockReason').hide();
            }
        });

        $('#blockDuration').change(function() {
            if ($(this).val() === 'other') {
                $('#customBlockDuration').show();
            } else {
                $('#customBlockDuration').hide();
            }
        });

        $('#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');
            var allowusertalk = $('#allowusertalkCheckbox').is(':checked');

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

    function blockUser(username, reason, duration, nocreate, noemail, allowusertalk) {
        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,
            allowusertalk: allowusertalk ? 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'
        );

        $(portletLink).click(function(e) {
            e.preventDefault(); // Prevent following the link
            // Change the browser URL without reloading the page
            if (history.pushState) {
                history.pushState(null, null, portletLink.href);
            }
            // Load the interface as a new "page"
            createBlockingInterface();
        });
    });
}