User:Euphoria/massBlock.js: Difference between revisions

From Test Wiki
Jump to navigation Jump to search
Content deleted Content added
Custom block duration and popup interface.
Undo revision 34168 by Euphoria (talk)
Tag: Undo
 
(8 intermediate revisions by the same user not shown)
Line 1: Line 1:
if (mw.config.get('wgUserGroups').includes('sysop')) {
if (mw.config.get('wgUserGroups').includes('sysop')) {


// Function to create the blocking interface in a new window
// Function to create the blocking interface as if it's a "Special Page"
function createBlockingInterface() {
function createBlockingInterface() {
// Change the document title in the browser tab
var windowFeatures = 'menubar=no,location=no,resizable=yes,scrollbars=yes,status=no,width=400,height=400';
document.title = 'Mass Block Tool - ' + mw.config.get('wgSiteName');
var massBlockWindow = window.open('', 'MassBlockWindow', windowFeatures);


// Change the content header to 'Mass Block Tool' and remove the subtitle
var formHtml = '<div id="massBlockForm" style="background: white; border: 1px solid black; padding: 10px;">'
$('#firstHeading').text('Mass Block Tool'); // This changes the main page title
+ '<h3>Mass Block</h3>'
$('#contentSub, #siteSub').hide(); // This hides the subtitle and the user link
+ '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>';


// The HTML for the blocking interface
massBlockWindow.document.body.innerHTML = formHtml;
var formHtml = '<div id="massBlockTool" style="margin: 20px;">' +
'<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"> Prevent user from editing their talk page<br>' +
'<button id="executeMassBlock" style="margin-top: 10px;">Block Users</button>' +
'</div>';


// Clear the current content and insert the new form
massBlockWindow.document.getElementById('executeMassBlock').onclick = function() {
var contentText = $('#mw-content-text');
var users = massBlockWindow.document.getElementById('usernamesToBlock').value.split(',');
contentText.empty();
var reason = massBlockWindow.document.getElementById('blockReason').value;
contentText.html(formHtml);
var duration = massBlockWindow.document.getElementById('blockDuration').value;

if (duration === 'custom') {
// Event handlers for form inputs and the block action
duration = massBlockWindow.document.getElementById('customDuration').value;
$('#blockReasonSelect').change(function() {
if ($(this).val() === 'other') {
$('#otherBlockReason').show();
} else {
$('#otherBlockReason').hide();
}
}
users.forEach(function(user) {
});
blockUser(user.trim(), reason, duration);
});
};


massBlockWindow.checkCustomDuration = function(value) {
$('#blockDuration').change(function() {
if (value === 'custom') {
if ($(this).val() === 'other') {
massBlockWindow.document.getElementById('customDuration').style.display = 'block';
$('#customBlockDuration').show();
} else {
} else {
massBlockWindow.document.getElementById('customDuration').style.display = 'none';
$('#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 to block a user
function blockUser(username, reason, duration) {
function blockUser(username, reason, duration, nocreate, noemail, allowusertalk) {
var api = new mw.Api();
var api = new mw.Api();
api.postWithToken('csrf', {
api.postWithToken('csrf', {
Line 60: Line 92:
expiry: duration,
expiry: duration,
reason: reason,
reason: reason,
nocreate: true,
nocreate: nocreate ? 1 : 0,
autoblock: true,
autoblock: true,
noemail: true
noemail: noemail ? 1 : 0,
allowusertalk: allowusertalk ? 1 : 0
}).done(function(data) {
}).done(function(data) {
console.log('User blocked: ' + username);
console.log('User blocked: ' + username);
// Optionally, refresh the list or provide feedback to the user here
}).fail(function(error) {
}).fail(function(error) {
console.log('Error blocking user: ' + username);
console.log('Error blocking user: ' + username);
// Optionally, provide error feedback to the user here
});
});
}
}


// Load the interface
// Add the Mass Block link to the sidebar
$(document).ready(function() {
$(document).ready(function() {
var portletLink = mw.util.addPortletLink(
// Add a button or link to open the blocking interface
'p-tb',
var openInterfaceButton = $('<button>').text('Open Mass Block Interface');
'#',
openInterfaceButton.click(createBlockingInterface);
'Mass Block',
$('body').append(openInterfaceButton);
'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
});
});
});
}
}

Latest revision as of 13:47, 28 December 2023

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

    // Function to create the blocking interface as if it's a "Special Page"
    function createBlockingInterface() {
        // Change the document title in the browser tab
        document.title = 'Mass Block Tool - ' + mw.config.get('wgSiteName');

        // Change the content header to 'Mass Block Tool' and remove the subtitle
        $('#firstHeading').text('Mass Block Tool'); // This changes the main page title
        $('#contentSub, #siteSub').hide(); // This hides the subtitle and the user link

        // The HTML for the blocking interface
        var formHtml = '<div id="massBlockTool" style="margin: 20px;">' +
            '<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"> Prevent user from editing their talk page<br>' +
            '<button id="executeMassBlock" style="margin-top: 10px;">Block Users</button>' +
            '</div>';

        // Clear the current content and insert the new form
        var contentText = $('#mw-content-text');
        contentText.empty();
        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
        });
    });
}