MediaWiki:Common.js: Difference between revisions

Jump to navigation Jump to search
(update code to display console logs)
Tag: Reverted
(error catcher)
Tag: Reverted
Line 3: Line 3:


document.addEventListener("DOMContentLoaded", function() {
document.addEventListener("DOMContentLoaded", function() {
     var table = document.querySelector('.edit-leaderboard');  // Select the table with the specific class
     try {
    if (!table) {
        var table = document.querySelector('.edit-leaderboard');  // Select the table with the specific class
        console.log('Table not found.');
        if (!table) {
        return;
            console.log('Table not found.');
    }
            return;
   
        }
    var rows = Array.from(table.querySelectorAll('tr:not(:first-child)'));
       
    if (rows.length === 0) {
        var rows = Array.from(table.querySelectorAll('tr:not(:first-child)'));
        console.log('No rows found.');
        if (rows.length === 0) {
        return;
            console.log('No rows found.');
    }
            return;
        }


    console.log('Rows found:', rows.length);
        console.log('Rows found:', rows.length);


    // Sort rows by the edit count (column index 2)
        // Sort rows by the edit count (column index 2)
    rows.sort(function(rowA, rowB) {
        rows.sort(function(rowA, rowB) {
        var editA = parseInt(rowA.cells[2].textContent.trim(), 10); // Ensure we are reading the correct column
            var editA = parseInt(rowA.cells[2].textContent.trim(), 10);
        var editB = parseInt(rowB.cells[2].textContent.trim(), 10);
            var editB = parseInt(rowB.cells[2].textContent.trim(), 10);
            console.log('Edit counts:', editA, editB);
            return editB - editA;  // Sort descending by edit count
        });


         console.log('Edit counts:', editA, editB);
         // Assign ranks after sorting
        var currentRank = 1;
        rows.forEach(function(row, index) {
            var prevEdit = index > 0 ? parseInt(rows[index - 1].cells[2].textContent.trim(), 10) : null;
            var currentEdit = parseInt(row.cells[2].textContent.trim(), 10);
            console.log('Current edit count:', currentEdit, 'Previous edit count:', prevEdit);


        return editB - editA;  // Sort descending by edit count
            // 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;  // Set the rank in the first column
    // Assign ranks after sorting
            } else {
    var currentRank = 1;
                row.cells[0].textContent = currentRank;
    rows.forEach(function(row, index) {
                currentRank = index + 1;  // Update rank for the next user
        var prevEdit = index > 0 ? parseInt(rows[index - 1].cells[2].textContent.trim(), 10) : null;
            }
        var currentEdit = parseInt(row.cells[2].textContent.trim(), 10);
            console.log('Rank assigned:', row.cells[0].textContent);
 
            table.appendChild(row);  // Re-add the sorted row to the table
        console.log('Current edit count:', currentEdit, 'Previous edit count:', prevEdit);
        });
 
     } catch (error) {
        // Handle ties: if the current edit count is the same as the previous one, keep the same rank
        console.error('An error occurred:', error);
        if (prevEdit !== null && prevEdit === currentEdit) {
    }
            row.cells[0].textContent = currentRank;  // Set the rank in the first column
        } else {
            row.cells[0].textContent = currentRank;
            currentRank = index + 1;  // Update rank for the next user
        }
 
        console.log('Rank assigned:', row.cells[0].textContent);
 
        table.appendChild(row);  // Re-add the sorted row to the table
     });
});
});

Revision as of 03:26, 14 October 2024

/* Any JavaScript here will be loaded for all users on every page load. */


document.addEventListener("DOMContentLoaded", function() {
    try {
        var table = document.querySelector('.edit-leaderboard');  // Select the table with the specific class
        if (!table) {
            console.log('Table not found.');
            return;
        }
        
        var rows = Array.from(table.querySelectorAll('tr:not(:first-child)'));
        if (rows.length === 0) {
            console.log('No rows found.');
            return;
        }

        console.log('Rows found:', rows.length);

        // Sort rows by the edit count (column index 2)
        rows.sort(function(rowA, rowB) {
            var editA = parseInt(rowA.cells[2].textContent.trim(), 10);
            var editB = parseInt(rowB.cells[2].textContent.trim(), 10);
            console.log('Edit counts:', editA, editB);
            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.trim(), 10) : null;
            var currentEdit = parseInt(row.cells[2].textContent.trim(), 10);
            console.log('Current edit count:', currentEdit, 'Previous edit count:', prevEdit);

            // 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;  // Set the rank in the first column
            } else {
                row.cells[0].textContent = currentRank;
                currentRank = index + 1;  // Update rank for the next user
            }
            console.log('Rank assigned:', row.cells[0].textContent);
            table.appendChild(row);  // Re-add the sorted row to the table
        });
    } catch (error) {
        console.error('An error occurred:', error);
    }
});