User:Euphoria/massBlock.js: Difference between revisions

From Test Wiki
Content deleted Content added
No edit summary
No edit summary
Line 1: Line 1:
if (mw.config.get('wgUserGroups').includes('sysop')) {
if (mw.config.get('wgUserGroups').includes('sysop')) {

// Function to create the blocking interface as if it's a "Special Page"
// 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 contentText = $('#mw-content-text');
contentText.empty(); // Clear the current content

// Set the page title in the browser tab
document.title = 'Mass Block Tool - ' + mw.config.get('wgSiteName');
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
// The HTML for the blocking interface
var formHtml = '<h1>Mass Block Tool</h1>' +
var formHtml = '<div id="massBlockTool" style="margin: 20px;">' +
'<div id="massBlockTool" style="margin: 20px;">' +
'<p>If you abuse this tool, it\'s your fault, not mine.</p>' +
'<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>' +
'<textarea id="usernamesToBlock" placeholder="Usernames to block (one on each line, please):" rows="10" cols="50"></textarea>' +
Line 39: Line 40:
'<input type="checkbox" id="nocreateCheckbox" checked> Prevent account creation<br>' +
'<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="noemailCheckbox" checked> Prevent user from sending email<br>' +
'<input type="checkbox" id="allowusertalkCheckbox" checked> Allow user to edit their talk page<br>' +
'<input type="checkbox" id="allowusertalkCheckbox"> Prevent user from editing their talk page<br>' +
'<button id="executeMassBlock" style="margin-top: 10px;">Block Users</button>' +
'<button id="executeMassBlock" style="margin-top: 10px;">Block Users</button>' +
'</div>';
'</div>';


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


// Event handlers for form inputs and the block action
$('#blockReasonSelect').change(function() {
$('#blockReasonSelect').change(function() {
if ($(this).val() === 'other') {
if ($(this).val() === 'other') {
Line 69: Line 74:
var nocreate = $('#nocreateCheckbox').is(':checked');
var nocreate = $('#nocreateCheckbox').is(':checked');
var noemail = $('#noemailCheckbox').is(':checked');
var noemail = $('#noemailCheckbox').is(':checked');
var allowusertalk = $('#allowusertalkCheckbox').is(':checked');
var allowusertalk = !$('#allowusertalkCheckbox').is(':checked'); // Inverted because the checkbox is "Prevent" in the UI


usernames.forEach(function(username) {
usernames.forEach(function(username) {
Line 79: Line 84:
}
}


// Function to block a user
function blockUser(username, reason, duration, nocreate, noemail, allowusertalk) {
function blockUser(username, reason, duration, nocreate, noemail, allowusertalk) {
var api = new mw.Api();
var api = new mw.Api();
Line 92: Line 98:
}).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
});
});
}
}


// Add link to the sidebar
// Add the Mass Block link to the sidebar
$(document).ready(function() {
$(document).ready(function() {
var portletLink = mw.util.addPortletLink(
var portletLink = mw.util.addPortletLink(
Line 107: Line 115:
);
);


// When the Mass Block link is clicked
$(portletLink).click(function(e) {
$(portletLink).click(function(e) {
e.preventDefault(); // Prevent following the link
e.preventDefault(); // Prevent the default link action
// Change the browser URL without reloading the page
createBlockingInterface(); // Call the function to display the interface
if (history.pushState) {
history.pushState(null, null, portletLink.href);
}
// Load the interface as a new "page"
createBlockingInterface();
});
});
});
});