MediaWiki:Common.js: Difference between revisions
Appearance
m Fix weirdly displaying numbers in leaderboard |
tweaked comments |
||
| Line 1: | Line 1: | ||
/* Any JavaScript here will be loaded for all users on every page load. */ | /* Any JavaScript here will be loaded for all users on every page load. */ | ||
function sortLeaderboard() { | function sortLeaderboard() { | ||
var table = document.querySelector('.edit-leaderboard'); // | var table = document.querySelector('.edit-leaderboard'); // Only affects wikitables with the ".edit-leaderboard" class | ||
// Check | // Check for table | ||
if (!table) { | if (!table) { | ||
return; // Exit if | return; // Exit if a table doesn't exist | ||
} | } | ||
// Select all rows excluding the header row | // Select all rows excluding the header row | ||
var headerRow = table.rows[0]; | var headerRow = table.rows[0]; | ||
var rows = Array.prototype.slice.call(table.querySelectorAll('tr')).filter(function(row) { | var rows = Array.prototype.slice.call(table.querySelectorAll('tr')).filter(function(row) { | ||
return row !== headerRow; | return row !== headerRow; | ||
}); | }); | ||
// Calculate and display total edits in the last column for each row | // Calculate and display total edits in the last column for each row | ||
rows.forEach(function(row) { | rows.forEach(function(row) { | ||
var totalEdits = calculateTotalEdits(row); | var totalEdits = calculateTotalEdits(row); | ||
if (row.cells[4]) { | if (row.cells[4]) { | ||
row.cells[4].innerHTML = '<strong style="font-weight: bold;">' + formatNumber(totalEdits) + '</strong>'; | row.cells[4].innerHTML = '<strong style="font-weight: bold;">' + formatNumber(totalEdits) + '</strong>'; | ||
} | } | ||
}); | }); | ||
// Sort rows by | // Sort rows descending by total edit count | ||
rows.sort(function(rowA, rowB) { | rows.sort(function(rowA, rowB) { | ||
var editA = calculateTotalEdits(rowA); | var editA = calculateTotalEdits(rowA); | ||
var editB = calculateTotalEdits(rowB); | var editB = calculateTotalEdits(rowB); | ||
return editB - editA; | return editB - editA; | ||
}); | }); | ||
// Update | // Update placements after sorting | ||
var | var currentPlacement = 1; | ||
var lastCount = -1; | var lastCount = -1; | ||
rows.forEach(function(row) { | rows.forEach(function(row) { | ||
var editCount = calculateTotalEdits(row); // Get current total edit count | var editCount = calculateTotalEdits(row); // Get current total edit count | ||
// Assign | // Assign placement | ||
if (editCount !== lastCount) { | if (editCount !== lastCount) { | ||
row.cells[0].innerHTML = '<strong style="font-weight: bold;">' + | row.cells[0].innerHTML = '<strong style="font-weight: bold;">' + currentPlacement + '</strong>'; // Assign current placement | ||
lastCount = editCount; | lastCount = editCount; | ||
currentPlacement++; // Increment placement for the next unique entry | |||
} else { | } else { | ||
row.cells[0].innerHTML = '<strong style="font-weight: bold;">' + ( | row.cells[0].innerHTML = '<strong style="font-weight: bold;">' + (currentPlacement - 1) + '</strong>'; // Keep the same placement for ties | ||
} | } | ||
}); | }); | ||
// Clear the table and re-add sorted rows | // Clear the table and re-add sorted rows excluding the header | ||
while (table.rows.length > 1) { | while (table.rows.length > 1) { | ||
table.deleteRow(1); | table.deleteRow(1); | ||
} | } | ||
// | // Add things back to the table | ||
table.appendChild(headerRow); | table.appendChild(headerRow); | ||
rows.forEach(function(row) { | rows.forEach(function(row) { | ||
table.appendChild(row); | table.appendChild(row); | ||
}); | }); | ||
} | } | ||
// | // Sort | ||
sortLeaderboard(); | sortLeaderboard(); | ||
// | // Calculate total edits for rows | ||
function calculateTotalEdits(row) { | function calculateTotalEdits(row) { | ||
var username = row.cells[1] ? row.cells[1].innerText.trim() : ""; // Get | var username = row.cells[1] ? row.cells[1].innerText.trim() : ""; // Get username without crashing if formatting is incorrect | ||
var totalEdits = 0; | var totalEdits = 0; | ||
// Get | // Get edit count | ||
var editCountsText = row.cells[2] ? row.cells[2].innerText.replace(/,/g, '').trim() : ""; // Remove commas | var editCountsText = row.cells[2] ? row.cells[2].innerText.replace(/,/g, '').trim() : ""; // Remove commas | ||
var editCounts = editCountsText.split(/\s+/).map(function(edit) { // Split only on whitespace | var editCounts = editCountsText.split(/\s+/).map(function(edit) { // Split only on whitespace | ||
return parseInt(edit, 10) || 0; // Convert to integer | return parseInt(edit, 10) || 0; // Convert to integer | ||
}); | }); | ||
// Get fandom | // Get fandom edit count | ||
var fandomEdits = row.cells[3] ? parseInt(row.cells[3].innerText.trim().replace(/[^0-9]/g, ''), 10) || 0 : 0; | var fandomEdits = row.cells[3] ? parseInt(row.cells[3].innerText.trim().replace(/[^0-9]/g, ''), 10) || 0 : 0; | ||
// Sum | // Sum | ||
totalEdits = editCounts.reduce((acc, count) => acc + count, 0) + fandomEdits; | totalEdits = editCounts.reduce((acc, count) => acc + count, 0) + fandomEdits; | ||
| Line 87: | Line 84: | ||
} | } | ||
// | // Add commas | ||
function formatNumber(num) { | function formatNumber(num) { | ||
return num.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ','); | return num.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ','); | ||
} | } | ||
Latest revision as of 00:09, 25 August 2025
/* Any JavaScript here will be loaded for all users on every page load. */
function sortLeaderboard() {
var table = document.querySelector('.edit-leaderboard'); // Only affects wikitables with the ".edit-leaderboard" class
// Check for table
if (!table) {
return; // Exit if a table doesn't exist
}
// Select all rows excluding the header row
var headerRow = table.rows[0];
var rows = Array.prototype.slice.call(table.querySelectorAll('tr')).filter(function(row) {
return row !== headerRow;
});
// Calculate and display total edits in the last column for each row
rows.forEach(function(row) {
var totalEdits = calculateTotalEdits(row);
if (row.cells[4]) {
row.cells[4].innerHTML = '<strong style="font-weight: bold;">' + formatNumber(totalEdits) + '</strong>';
}
});
// Sort rows descending by total edit count
rows.sort(function(rowA, rowB) {
var editA = calculateTotalEdits(rowA);
var editB = calculateTotalEdits(rowB);
return editB - editA;
});
// Update placements after sorting
var currentPlacement = 1;
var lastCount = -1;
rows.forEach(function(row) {
var editCount = calculateTotalEdits(row); // Get current total edit count
// Assign placement
if (editCount !== lastCount) {
row.cells[0].innerHTML = '<strong style="font-weight: bold;">' + currentPlacement + '</strong>'; // Assign current placement
lastCount = editCount;
currentPlacement++; // Increment placement for the next unique entry
} else {
row.cells[0].innerHTML = '<strong style="font-weight: bold;">' + (currentPlacement - 1) + '</strong>'; // Keep the same placement for ties
}
});
// Clear the table and re-add sorted rows excluding the header
while (table.rows.length > 1) {
table.deleteRow(1);
}
// Add things back to the table
table.appendChild(headerRow);
rows.forEach(function(row) {
table.appendChild(row);
});
}
// Sort
sortLeaderboard();
// Calculate total edits for rows
function calculateTotalEdits(row) {
var username = row.cells[1] ? row.cells[1].innerText.trim() : ""; // Get username without crashing if formatting is incorrect
var totalEdits = 0;
// Get edit count
var editCountsText = row.cells[2] ? row.cells[2].innerText.replace(/,/g, '').trim() : ""; // Remove commas
var editCounts = editCountsText.split(/\s+/).map(function(edit) { // Split only on whitespace
return parseInt(edit, 10) || 0; // Convert to integer
});
// Get fandom edit count
var fandomEdits = row.cells[3] ? parseInt(row.cells[3].innerText.trim().replace(/[^0-9]/g, ''), 10) || 0 : 0;
// Sum
totalEdits = editCounts.reduce((acc, count) => acc + count, 0) + fandomEdits;
return totalEdits;
}
// Add commas
function formatNumber(num) {
return num.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',');
}
// Fix downscaling type on map pages
if (/^https:\/\/wiki\.pxls\.space\/(index\.php\?title=Map:|view\/Map:)/.test(window.location.href)) {
var style = document.createElement('style');
style.innerHTML = `
#map_leaflet_1 {
image-rendering: -moz-crisp-edges;
image-rendering: -o-crisp-edges;
image-rendering: -webkit-optimize-contrast;
-ms-interpolation-mode: nearest-neighbor;
image-rendering: pixelated;
}
`;
document.head.appendChild(style);
}