Jump to content

User:94rain/Gadget-MarkRights.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
$(function () {
    var groups = {
        // 全站管理型权限
        bureaucrat: {list: [], class: "markrights-bureaucrat"},
        checkuser: {list: [], class: "markrights-checkuser"},
        oversight: {list: [], class: "markrights-oversight"},
        sysop: {list: [], class: "markrights-sysop"},
        'interface-admin': {list: [], class: "markrights-interface-admin"},
        // 页面管理型权限
        patroller: {list: [], class: "markrights-patroller"},
        rollbacker: {list: [], class: "markrights-rollbacker"},
        autoreviewer: {list: [], class: "markrights-autoreviewer"},
        // 大量操作型权限
        accountcreator: {list: [], class: "markrights-accountcreator"},
        'massmessage-sender': {list: [], class: "markrights-massmessage-sender"},

    };
    var markUG = function () {
        var $users = $('a.mw-userlink:not(.mw-anonuserlink)');
        var users = {};
        $users.each(function (index, link) {
            users[link.textContent] = true;
        });

        var queue1 = [];
        var queue2 = [];
        var i=0, n=0;
        for (var user in users) {
            queue1.push(user);
            i++;
            if (i === 50) {
                queue2.push(queue1);
                queue1 = [];
                n++;
                i = 0;
            }
        }
        if (queue1.length > 0) {
            queue2.push(queue1);
            n++;
        }

        var done = function () {
            var group, user, j;
            for (group in groups) {
                if (groups.hasOwnProperty(group)) {
                    for (j=0; j<groups[group].list.length; j++) {
                        $('a.mw-userlink[title="User:' + groups[group].list[j] + '"]').append('<sup class="' + groups[group].class + '"></sup>');
                    }
                }
            }
        };

        var process = function (data) {
            var users, group;
            if (data.query && data.query.users) {
                users = data.query.users;
            } else {
                users = [];
            }
            for (var i=0; i<users.length; i++) {
                var user = users[i];
                if (user.groups) {
                    for (group in groups) {
                        if (groups.hasOwnProperty(group) && user.groups.indexOf(group) > -1) {
                            groups[group].list.push(user.name);
                        }
                    }
                }
            }
            n--;
            if (n <= 0) {
                done();
            }
        };
        var api = new mw.Api();
        for (var j=0; j<queue2.length; j++) {
            api.get({
                format: 'json',
                action: 'query',
                list: 'users',
                usprop: 'groups',
                ususers: queue2[j].join('|')
            }).done(process);
        }
    };
    markUG();
});