User:Euphoria/massBlock.js: Difference between revisions

From Test Wiki
Jump to navigation Jump to search
Content deleted Content added
massBlock
 
Custom block duration and popup interface.
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 in a new window
function createBlockingInterface() {
function createBlockingInterface() {
var windowFeatures = 'menubar=no,location=no,resizable=yes,scrollbars=yes,status=no,width=400,height=400';
var formHtml = '<div id="massBlockForm" style="position: fixed; bottom: 10px; right: 10px; background: white; border: 1px solid black; padding: 10px;">'
var massBlockWindow = window.open('', 'MassBlockWindow', windowFeatures);

var formHtml = '<div id="massBlockForm" style="background: white; border: 1px solid black; padding: 10px;">'
+ '<h3>Mass Block</h3>'
+ '<h3>Mass Block</h3>'
+ 'Usernames (comma-separated):<br><textarea id="usernamesToBlock" rows="2" cols="20"></textarea><br>'
+ 'Usernames (comma-separated):<br><textarea id="usernamesToBlock" rows="2" cols="20"></textarea><br>'
+ 'Block reason:<br><input type="text" id="blockReason"><br>'
+ 'Block reason:<br><input type="text" id="blockReason"><br>'
+ 'Block duration:<br>'
+ 'Block duration:<br>'
+ '<select id="blockDuration">'
+ '<select id="blockDuration" onchange="checkCustomDuration(this.value)">'
+ '<option value="6 hours">6 hours</option>'
+ '<option value="6 hours">6 hours</option>'
+ '<option value="24 hours">24 hours</option>'
+ '<option value="24 hours">24 hours</option>'
Line 21: Line 23:
+ '<option value="5 years">5 years</option>'
+ '<option value="5 years">5 years</option>'
+ '<option value="indefinite">indefinite</option>'
+ '<option value="indefinite">indefinite</option>'
+ '<option value="custom">Custom</option>'
+ '</select><br>'
+ '</select><br>'
+ '<input type="text" id="customDuration" style="display:none;" placeholder="Enter duration"><br>'
+ '<button id="executeMassBlock">Block Users</button>'
+ '<button id="executeMassBlock">Block Users</button>'
+ '</div>';
+ '</div>';


$('body').append(formHtml);
massBlockWindow.document.body.innerHTML = formHtml;


$('#executeMassBlock').click(function() {
massBlockWindow.document.getElementById('executeMassBlock').onclick = function() {
var users = $('#usernamesToBlock').val().split(',');
var users = massBlockWindow.document.getElementById('usernamesToBlock').value.split(',');
var reason = $('#blockReason').val();
var reason = massBlockWindow.document.getElementById('blockReason').value;
var duration = $('#blockDuration').val();
var duration = massBlockWindow.document.getElementById('blockDuration').value;
if (duration === 'custom') {
duration = massBlockWindow.document.getElementById('customDuration').value;
}
users.forEach(function(user) {
users.forEach(function(user) {
blockUser(user.trim(), reason, duration);
blockUser(user.trim(), reason, duration);
});
});
});
};

massBlockWindow.checkCustomDuration = function(value) {
if (value === 'custom') {
massBlockWindow.document.getElementById('customDuration').style.display = 'block';
} else {
massBlockWindow.document.getElementById('customDuration').style.display = 'none';
}
};
}
}


Line 57: Line 72:
// Load the interface
// Load the interface
$(document).ready(function() {
$(document).ready(function() {
// Add a button or link to open the blocking interface
createBlockingInterface();
var openInterfaceButton = $('<button>').text('Open Mass Block Interface');
openInterfaceButton.click(createBlockingInterface);
$('body').append(openInterfaceButton);
});
});
}
}

Revision as of 11:44, 28 December 2023

if (mw.config.get('wgUserGroups').includes('sysop')) {

    // Function to create the blocking interface in a new window
    function createBlockingInterface() {
        var windowFeatures = 'menubar=no,location=no,resizable=yes,scrollbars=yes,status=no,width=400,height=400';
        var massBlockWindow = window.open('', 'MassBlockWindow', windowFeatures);

        var formHtml = '<div id="massBlockForm" style="background: white; border: 1px solid black; padding: 10px;">'
            + '<h3>Mass Block</h3>'
            + 'Usernames (comma-separated):<br><textarea id="usernamesToBlock" rows="2" cols="20"></textarea><br>'
            + 'Block reason:<br><input type="text" id="blockReason"><br>'
            + 'Block duration:<br>'
            + '<select id="blockDuration" onchange="checkCustomDuration(this.value)">'
            + '<option value="6 hours">6 hours</option>'
            + '<option value="24 hours">24 hours</option>'
            + '<option value="31 hours">31 hours</option>'
            + '<option value="1 week">1 week</option>'
            + '<option value="2 weeks">2 weeks</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="5 years">5 years</option>'
            + '<option value="indefinite">indefinite</option>'
            + '<option value="custom">Custom</option>'
            + '</select><br>'
            + '<input type="text" id="customDuration" style="display:none;" placeholder="Enter duration"><br>'
            + '<button id="executeMassBlock">Block Users</button>'
            + '</div>';

        massBlockWindow.document.body.innerHTML = formHtml;

        massBlockWindow.document.getElementById('executeMassBlock').onclick = function() {
            var users = massBlockWindow.document.getElementById('usernamesToBlock').value.split(',');
            var reason = massBlockWindow.document.getElementById('blockReason').value;
            var duration = massBlockWindow.document.getElementById('blockDuration').value;
            if (duration === 'custom') {
                duration = massBlockWindow.document.getElementById('customDuration').value;
            }
            users.forEach(function(user) {
                blockUser(user.trim(), reason, duration);
            });
        };

        massBlockWindow.checkCustomDuration = function(value) {
            if (value === 'custom') {
                massBlockWindow.document.getElementById('customDuration').style.display = 'block';
            } else {
                massBlockWindow.document.getElementById('customDuration').style.display = 'none';
            }
        };
    }

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

    // Load the interface
    $(document).ready(function() {
        // Add a button or link to open the blocking interface
        var openInterfaceButton = $('<button>').text('Open Mass Block Interface');
        openInterfaceButton.click(createBlockingInterface);
        $('body').append(openInterfaceButton);
    });
}