User:Euphoria/massBlock.js

From Test Wiki
Revision as of 13:46, 28 December 2023 by Euphoria (talk | contribs)
Jump to navigation Jump to search

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 change the title and hide the user path
    function changeTitleAndHideUserPath() {
        document.title = 'Mass Block Tool - ' + mw.config.get('wgSiteName');
        $('#firstHeading').text('Mass Block Tool'); // Changes the main title on the page
        $('#contentSub').remove(); // Removes the subtitle which typically includes user path
        // Depending on your MediaWiki skin, you might need to change the selectors above
    }

    // Function to create the blocking interface
    function createBlockingInterface() {
        changeTitleAndHideUserPath(); // Call the function to change title and hide the user path

        // Clear the content area and insert the form
        var contentText = $('#mw-content-text');
        contentText.empty();

        var formHtml = '<div id="massBlockTool" style="margin: 20px;">' +
            // ... (form HTML as before)
        contentText.html(formHtml);

        // Event handlers for form inputs and the block action
        $('#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'); // Inverted because the checkbox is "Prevent" in the UI

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

    // Function to block a user
    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);
            // Optionally, refresh the list or provide feedback to the user here
        }).fail(function(error) {
            console.log('Error blocking user: ' + username);
            // Optionally, provide error feedback to the user here
        });
    }

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

        // When the Mass Block link is clicked
        $(portletLink).click(function(e) {
            e.preventDefault(); // Prevent the default link action
            createBlockingInterface(); // Call the function to display the interface
        });
    });
}