MediaWiki:Common.js: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
(Add auto-sorting leaderboard code) Tag: Reverted |
||
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. */ | ||
document.addEventListener("DOMContentLoaded", function() { | |||
var table = document.querySelector('.edit-leaderboard'); // Select only the table with this specific class | |||
if (!table) return; | |||
var rows = Array.from(table.querySelectorAll('tr:not(:first-child)')); | |||
rows.sort(function(rowA, rowB) { | |||
var editA = parseInt(rowA.cells[2].textContent, 10); // Edit count is in the 3rd column | |||
var editB = parseInt(rowB.cells[2].textContent, 10); | |||
return editB - editA; // Sort descending by edit count | |||
}); | |||
// Assign ranks after sorting | |||
var currentRank = 1; | |||
rows.forEach(function(row, index) { | |||
var prevEdit = index > 0 ? parseInt(rows[index - 1].cells[2].textContent, 10) : null; | |||
var currentEdit = parseInt(row.cells[2].textContent, 10); | |||
// Handle ties: if the current edit count is the same as the previous one, keep the same rank | |||
if (prevEdit !== null && prevEdit === currentEdit) { | |||
row.cells[0].textContent = currentRank; | |||
} else { | |||
row.cells[0].textContent = currentRank; | |||
currentRank = index + 1; // Update rank for the next user | |||
} | |||
table.appendChild(row); // Re-add the sorted row to the table | |||
}); | |||
}); |
Revision as of 03:14, 14 October 2024
/* Any JavaScript here will be loaded for all users on every page load. */ document.addEventListener("DOMContentLoaded", function() { var table = document.querySelector('.edit-leaderboard'); // Select only the table with this specific class if (!table) return; var rows = Array.from(table.querySelectorAll('tr:not(:first-child)')); rows.sort(function(rowA, rowB) { var editA = parseInt(rowA.cells[2].textContent, 10); // Edit count is in the 3rd column var editB = parseInt(rowB.cells[2].textContent, 10); return editB - editA; // Sort descending by edit count }); // Assign ranks after sorting var currentRank = 1; rows.forEach(function(row, index) { var prevEdit = index > 0 ? parseInt(rows[index - 1].cells[2].textContent, 10) : null; var currentEdit = parseInt(row.cells[2].textContent, 10); // Handle ties: if the current edit count is the same as the previous one, keep the same rank if (prevEdit !== null && prevEdit === currentEdit) { row.cells[0].textContent = currentRank; } else { row.cells[0].textContent = currentRank; currentRank = index + 1; // Update rank for the next user } table.appendChild(row); // Re-add the sorted row to the table }); });