MediaWiki:Gadget-UserEditCount.js

From Inkipedia, the Splatoon wiki

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
function populateCountUserboxen() {
    function getUserEdits(username) {
        var api = new mw.Api();

        function fetchEdits(username, uccontinue, totalEdits) {
            totalEdits = totalEdits || 0;
            return new Promise(function(resolve, reject) {
                api.get({
                    action: 'query',
                    list: 'usercontribs',
                    ucuser: username,
                    ucnamespace: '0|1|4|5|6|7|8|9|10|11|12|13|14|15|828|829|2300|2301|2302|2303|3000|3001', // Exclude user (2) and user_talk (3) namespaces
                    uclimit: 'max',
                    uccontinue: uccontinue
                }).done(function(data) {
                    totalEdits  = data.query.usercontribs.length;
    
                    if (data.continue) {
                        fetchEdits(username, data.continue.uccontinue, totalEdits).then(resolve).catch(reject);
                    } else {
                        resolve(totalEdits);
                    }
                }).fail(function(error) {
                    reject('Error retrieving user contributions: '   error);
                });
            });
        }

        return fetchEdits(username);
    }

    var editCountUserboxen = document.querySelectorAll('.edit-count-userbox');
    Array.prototype.forEach.call(editCountUserboxen, function(userbox) {
        var username = userbox.querySelector('.edit-count-userbox-username').innerText;
        if (username) {
            getUserEdits(username).then(function(count) {
                var className = '';
                switch (true) {
                    case (count >= 100000):
                        className = 'user-count-100000';
                        break;
                    case (count >= 50000):
                        className = 'user-count-50000';
                        break;
                    case (count >= 10000):
                        className = 'user-count-10000';
                        break;
                    case (count >= 5000):
                        className = 'user-count-5000';
                        break;
                    case (count >= 2000):
                        className = 'user-count-2000';
                        break;
                    case (count >= 1000):
                        className = 'user-count-1000';
                        break;
                    case (count >= 500):
                        className = 'user-count-500';
                        break;
                    case (count >= 250):
                        className = 'user-count-250';
                        break;
                    case (count >= 100):
                        className = 'user-count-100';
                        break;
                    case (count >= 50):
                        className = 'user-count-50';
                        break;
                    case (count >= 10):
                        className = 'user-count-10';
                        break;
                    default:
                        className = 'user-count-0';
                }
                
                userbox.classList.add(className);
                var countTextElements = userbox.querySelectorAll('.edit-count-userbox-count');
                Array.prototype.forEach.call(countTextElements, function(countText) {
                    countText.innerText = count;
                });
            });
        }
    });
}

populateCountUserboxen();