User:Euphoria/massBlock.js: Difference between revisions
From Test Wiki
Content deleted Content added
No edit summary |
Tag: Undo |
||
| (6 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 |
// 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 |
|||
| ⚫ | |||
document.title = 'Mass Block Tool - ' + mw.config.get('wgSiteName'); |
|||
| ⚫ | |||
| ⚫ | |||
+ 'Block reason:<br><input type="text" id="blockReason"><br>' |
|||
| ⚫ | |||
| ⚫ | |||
| ⚫ | |||
| ⚫ | |||
| ⚫ | |||
| ⚫ | |||
| ⚫ | |||
| ⚫ | |||
| ⚫ | |||
| ⚫ | |||
| ⚫ | |||
| ⚫ | |||
| ⚫ | |||
| ⚫ | |||
| ⚫ | |||
// |
// 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 |
|||
| ⚫ | |||
'<p>If you abuse this tool, it\'s your fault, not mine.</p>' + |
|||
| ⚫ | |||
| ⚫ | |||
'<select id="blockReasonSelect">' + |
|||
'<option value="vandalism">Vandalism</option>' + |
|||
| ⚫ | |||
'<option value="harassment">Harassment</option>' + |
|||
'<option value="other">Other reason</option>' + |
|||
| ⚫ | |||
'<input type="text" id="otherBlockReason" placeholder="Other/additional reason" style="display:none; margin-top: 10px;">' + |
|||
'<h3>Block duration:</h3>' + |
|||
| ⚫ | |||
| ⚫ | |||
| ⚫ | |||
| ⚫ | |||
| ⚫ | |||
| ⚫ | |||
| ⚫ | |||
| ⚫ | |||
| ⚫ | |||
| ⚫ | |||
| ⚫ | |||
'<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>' + |
|||
| ⚫ | |||
| ⚫ | |||
// Clear the current content and insert the new form |
|||
var contentText = $('#mw-content-text'); |
|||
contentText.empty(); |
|||
| ⚫ | |||
// Event handlers for form inputs and the block action |
|||
$('#blockReasonSelect').change(function() { |
|||
if ($(this).val() === 'other') { |
|||
$('#otherBlockReason').show(); |
|||
} else { |
|||
$('#otherBlockReason').hide(); |
|||
} |
|||
| ⚫ | |||
| ⚫ | |||
if ($(this).val() === 'other') { |
|||
$('#customBlockDuration').show(); |
|||
} else { |
|||
$('#customBlockDuration').hide(); |
|||
} |
|||
}); |
|||
$('#executeMassBlock').click(function() { |
$('#executeMassBlock').click(function() { |
||
var |
var usernames = $('#usernamesToBlock').val().split('\n'); |
||
var |
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); |
|||
} |
|||
}); |
}); |
||
}); |
}); |
||
| Line 38: | Line 85: | ||
// 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 45: | Line 92: | ||
expiry: duration, |
expiry: duration, |
||
reason: reason, |
reason: reason, |
||
nocreate: |
nocreate: nocreate ? 1 : 0, |
||
autoblock: true, |
autoblock: true, |
||
noemail: |
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 |
|||
}); |
}); |
||
} |
} |
||
// 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( |
||
'p-tb', |
'p-tb', |
||
'#', |
'#', |
||
'Mass Block', |
'Mass Block', |
||
't-massblock', |
't-massblock', |
||
'Mass block users' |
'Mass block users' |
||
); |
); |
||
// |
// When the Mass Block link is clicked |
||
$(portletLink).click(function(e) { |
$(portletLink).click(function(e) { |
||
e.preventDefault(); // Prevent the default action |
e.preventDefault(); // Prevent the default link action |
||
createBlockingInterface(); // Call the function to |
createBlockingInterface(); // Call the function to display the interface |
||
}); |
}); |
||
}); |
}); |
||