MediaWiki:Common.js: Difference between revisions
Jump to navigation
Jump to search
m (fix) |
(Add secondary sorting step to arrange users in the same placement alphabetically) |
||
(6 intermediate revisions by the same user not shown) | |||
Line 7: | Line 7: | ||
// Check if the table exists | // Check if the table exists | ||
if (!table) { | if (!table) { | ||
// console.log('No leaderboard table found with the specified class.'); | |||
return; // Exit if the table doesn't exist | return; // Exit if the table doesn't exist | ||
} | } | ||
// Select all rows excluding the header row | // Select all rows excluding the header row | ||
var rows = Array. | var headerRow = table.rows[0]; // Get the header row | ||
console.log('Rows found:', rows.length); // Log the number of rows found | var rows = Array.prototype.slice.call(table.querySelectorAll('tr')).filter(function(row) { | ||
return row !== headerRow; // Filter out the header row | |||
}); | |||
// console.log('Rows found:', rows.length); // Log the number of rows found | |||
// 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(row | rows.forEach(function(row) { | ||
var totalEdits = calculateTotalEdits(row); // Calculate total edits | var totalEdits = calculateTotalEdits(row); // Calculate total edits | ||
if (row.cells[4]) { | if (row.cells[4]) { | ||
row.cells[4].innerHTML = | row.cells[4].innerHTML = '<strong>' + formatNumber(totalEdits) + '</strong>'; // Update the Total Edits column with bold formatting and comma separation | ||
} | } | ||
}); | }); | ||
// Sort rows by the edit count (column index 4 for Total Edits) | // Sort rows by the edit count and username alphabetically (column index 4 for Total Edits) | ||
rows.sort((rowA, rowB) | rows.sort(function(rowA, rowB) { | ||
var editA = calculateTotalEdits(rowA); // Use the new function to get total edits | var editA = calculateTotalEdits(rowA); // Use the new function to get total edits | ||
var editB = calculateTotalEdits(rowB); // Use the new function to get total edits | var editB = calculateTotalEdits(rowB); // Use the new function to get total edits | ||
return editB - editA; | // Primary sort by edit count (descending) | ||
if (editA !== editB) return editB - editA; | |||
// Secondary sort by username (alphabetical ascending) | |||
var userA = rowA.cells[1].innerText.toLowerCase(); | |||
var userB = rowB.cells[1].innerText.toLowerCase(); | |||
return userA.localeCompare(userB); | |||
}); | }); | ||
// console.log('Rows sorted by edit count.'); // Log after sorting | |||
// Update ranks after sorting, handling ties | // Update ranks after sorting, handling ties | ||
var currentRank = 1; // Start from rank 1 | |||
var lastCount = -1; // Initialize lastCount to a value that won't match any edit count | |||
rows.forEach((row) | rows.forEach(function(row) { | ||
var editCount = calculateTotalEdits(row); // Get current total edit count | var editCount = calculateTotalEdits(row); // Get current total edit count | ||
// Assign rank: if current edit count is the same as the last, keep the same rank | // Assign rank: if current edit count is the same as the last, keep the same rank | ||
if (editCount !== lastCount) { | if (editCount !== lastCount) { | ||
row.cells[0].innerHTML = | row.cells[0].innerHTML = '<strong>' + currentRank + '</strong>'; // Assign current rank with bold formatting | ||
lastCount = editCount; // Update lastCount to the current edit count | lastCount = editCount; // Update lastCount to the current edit count | ||
currentRank++; // Increment rank for the next unique entry | currentRank++; // Increment rank for the next unique entry | ||
} else { | } else { | ||
row.cells[0].innerHTML = | row.cells[0].innerHTML = '<strong>' + (currentRank - 1) + '</strong>'; // Keep the same rank for ties | ||
} | } | ||
}); | }); | ||
// console.log('Ranks updated.'); // Log after updating ranks | |||
// Clear the table and re-add sorted rows | // Clear the table and re-add sorted rows | ||
while (table.rows.length > 1) { | while (table.rows.length > 1) { | ||
table.deleteRow(1); // Remove existing rows except for the header | table.deleteRow(1); // Remove existing rows except for the header | ||
// console.log('Row removed. Remaining rows:', table.rows.length); // Log remaining rows | |||
} | } | ||
// Append the header row back to the table first | |||
table.appendChild(headerRow); | |||
// Append sorted rows back to the table | // Append sorted rows back to the table | ||
rows.forEach((row) | rows.forEach(function(row) { | ||
table.appendChild(row); | table.appendChild(row); // Re-add the sorted row to the table | ||
}); | }); | ||
// console.log('Sorted rows added back to the table.'); // Log after adding rows | |||
// console.log('Leaderboard sorted and ranks updated.'); | |||
} | } | ||
// Execute the sorting function | // Execute the sorting function | ||
sortLeaderboard(); | sortLeaderboard(); | ||
// Function to calculate total edits for a row | // Function to calculate total edits for a row | ||
function calculateTotalEdits(row) { | function calculateTotalEdits(row) { | ||
var username = row.cells[1] ? row.cells[1].innerText : ""; // Get the username safely | var username = row.cells[1] ? row.cells[1].innerText : ""; // Get the username safely | ||
// console.log('Calculating total edits for user: ' + username); // Log the username | |||
// Initialize total edits | // Initialize total edits | ||
Line 81: | Line 91: | ||
// If the username contains numbers, use this logic | // If the username contains numbers, use this logic | ||
var editCountText = row.cells[2] ? row.cells[2].innerHTML.trim() : ""; // Safely access the cell | var editCountText = row.cells[2] ? row.cells[2].innerHTML.trim() : ""; // Safely access the cell | ||
// console.log('Edit count text: ' + editCountText); // Log the edit count text | |||
// Extract numbers from the edit count text (ignoring {{Special:Editcount/...}}) | // Extract numbers from the edit count text (ignoring {{Special:Editcount/...}}) | ||
var editCountMatch = editCountText.match(/>(\d+)<\/a>/g); // Match all edit count occurrences | var editCountMatch = editCountText.match(/>(\d+)<\/a>/g); // Match all edit count occurrences | ||
var edits = editCountMatch ? editCountMatch.map(match | var edits = editCountMatch ? editCountMatch.map(function(match) { | ||
return parseInt(match.match(/>(\d+)<\/a>/)[1], 10); | |||
}).reduce(function(a, b) { | |||
return a + b; | |||
}, 0) : 0; // Sum all edits | |||
// Get fandom edits from the "Fandom edits" cell (index 3) | // Get fandom edits from the "Fandom edits" cell (index 3) | ||
Line 95: | Line 109: | ||
// If the username does not contain numbers, use this logic | // If the username does not contain numbers, use this logic | ||
// Split the edit counts by line breaks and parse them | // Split the edit counts by line breaks and parse them | ||
var editCounts = row.cells[2] ? row.cells[2].innerHTML.trim().split('<br>').map(userEdit | var editCounts = row.cells[2] ? row.cells[2].innerHTML.trim().split('<br>').map(function(userEdit) { | ||
return parseInt(userEdit.replace(/[^0-9]/g, ''), 10) || 0; // Parse the current edits | return parseInt(userEdit.replace(/[^0-9]/g, ''), 10) || 0; // Parse the current edits | ||
}) : []; // Safely access the cell | }) : []; // Safely access the cell | ||
Line 102: | Line 116: | ||
// Sum the edits for all users and add fandom edits | // Sum the edits for all users and add fandom edits | ||
totalEdits = editCounts.reduce((acc, count) | totalEdits = editCounts.reduce(function(acc, count) { | ||
return acc + count; | |||
}, 0) + fandomEdits; | |||
return totalEdits; // Return the total edits | return totalEdits; // Return the total edits |
Latest revision as of 10:38, 9 November 2024
/* Any JavaScript here will be loaded for all users on every page load. */ // Function to sort leaderboard function sortLeaderboard() { var table = document.querySelector('.edit-leaderboard'); // Target the specific class // Check if the table exists if (!table) { // console.log('No leaderboard table found with the specified class.'); return; // Exit if the table doesn't exist } // Select all rows excluding the header row var headerRow = table.rows[0]; // Get the header row var rows = Array.prototype.slice.call(table.querySelectorAll('tr')).filter(function(row) { return row !== headerRow; // Filter out the header row }); // console.log('Rows found:', rows.length); // Log the number of rows found // Calculate and display total edits in the last column for each row rows.forEach(function(row) { var totalEdits = calculateTotalEdits(row); // Calculate total edits if (row.cells[4]) { row.cells[4].innerHTML = '<strong>' + formatNumber(totalEdits) + '</strong>'; // Update the Total Edits column with bold formatting and comma separation } }); // Sort rows by the edit count and username alphabetically (column index 4 for Total Edits) rows.sort(function(rowA, rowB) { var editA = calculateTotalEdits(rowA); // Use the new function to get total edits var editB = calculateTotalEdits(rowB); // Use the new function to get total edits // Primary sort by edit count (descending) if (editA !== editB) return editB - editA; // Secondary sort by username (alphabetical ascending) var userA = rowA.cells[1].innerText.toLowerCase(); var userB = rowB.cells[1].innerText.toLowerCase(); return userA.localeCompare(userB); }); // console.log('Rows sorted by edit count.'); // Log after sorting // Update ranks after sorting, handling ties var currentRank = 1; // Start from rank 1 var lastCount = -1; // Initialize lastCount to a value that won't match any edit count rows.forEach(function(row) { var editCount = calculateTotalEdits(row); // Get current total edit count // Assign rank: if current edit count is the same as the last, keep the same rank if (editCount !== lastCount) { row.cells[0].innerHTML = '<strong>' + currentRank + '</strong>'; // Assign current rank with bold formatting lastCount = editCount; // Update lastCount to the current edit count currentRank++; // Increment rank for the next unique entry } else { row.cells[0].innerHTML = '<strong>' + (currentRank - 1) + '</strong>'; // Keep the same rank for ties } }); // console.log('Ranks updated.'); // Log after updating ranks // Clear the table and re-add sorted rows while (table.rows.length > 1) { table.deleteRow(1); // Remove existing rows except for the header // console.log('Row removed. Remaining rows:', table.rows.length); // Log remaining rows } // Append the header row back to the table first table.appendChild(headerRow); // Append sorted rows back to the table rows.forEach(function(row) { table.appendChild(row); // Re-add the sorted row to the table }); // console.log('Sorted rows added back to the table.'); // Log after adding rows // console.log('Leaderboard sorted and ranks updated.'); } // Execute the sorting function sortLeaderboard(); // Function to calculate total edits for a row function calculateTotalEdits(row) { var username = row.cells[1] ? row.cells[1].innerText : ""; // Get the username safely // console.log('Calculating total edits for user: ' + username); // Log the username // Initialize total edits var totalEdits = 0; // Check if the username contains any numbers if (/\d/.test(username)) { // If the username contains numbers, use this logic var editCountText = row.cells[2] ? row.cells[2].innerHTML.trim() : ""; // Safely access the cell // console.log('Edit count text: ' + editCountText); // Log the edit count text // Extract numbers from the edit count text (ignoring {{Special:Editcount/...}}) var editCountMatch = editCountText.match(/>(\d+)<\/a>/g); // Match all edit count occurrences var edits = editCountMatch ? editCountMatch.map(function(match) { return parseInt(match.match(/>(\d+)<\/a>/)[1], 10); }).reduce(function(a, b) { return a + b; }, 0) : 0; // Sum all edits // Get fandom edits from the "Fandom edits" cell (index 3) var fandomEdits = row.cells[3] ? parseInt(row.cells[3].innerHTML.trim().replace(/[^0-9]/g, ''), 10) || 0 : 0; // Safely access the cell // Return the total edits return edits + fandomEdits; // Return total edits } else { // If the username does not contain numbers, use this logic // Split the edit counts by line breaks and parse them var editCounts = row.cells[2] ? row.cells[2].innerHTML.trim().split('<br>').map(function(userEdit) { return parseInt(userEdit.replace(/[^0-9]/g, ''), 10) || 0; // Parse the current edits }) : []; // Safely access the cell var fandomEdits = row.cells[3] ? parseInt(row.cells[3].innerHTML.trim().replace(/[^0-9]/g, ''), 10) || 0 : 0; // Safely access the cell // Sum the edits for all users and add fandom edits totalEdits = editCounts.reduce(function(acc, count) { return acc + count; }, 0) + fandomEdits; return totalEdits; // Return the total edits } } // Function to format numbers with commas function formatNumber(num) { return num.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ','); // Format number with commas }