跳转到内容

User:Vanished user 1929210/js/現在我可以搞破壊了嗎.js

维基百科,自由的百科全书
注意:保存之后,你必须清除浏览器缓存才能看到做出的更改。Google ChromeFirefoxMicrosoft EdgeSafari:按住⇧ Shift键并单击工具栏的“刷新”按钮。参阅Help:绕过浏览器缓存以获取更多帮助。
/**
 * 取自[[User:Alexander_Misel/admin.js]]
 * 把選單移到了「更多」,並改造成破壞者必備用品
 */

(function($, mw) {
    // Create portlet link
    var portletLinkOnline = mw.util.addPortletLink(
        'p-cactions',
        '#',
        '現在我可以搞破壞了嗎?'
    );

    var rcstart, rcend, time;
    var users = [];
    var admins = [], rollbackers = [], patrollers = [];
    var api = new mw.Api();

    // Bind click handler
    $(portletLinkOnline).click(function(e) {
        e.preventDefault();

        users = [];
        var usersExt = [];
        admins = [];
        rollbackers = [];
        patrollers = [];

        // 最近更改30分钟内的编辑用户
        time = new Date();
        rcstart = time.toISOString();
        time.setMinutes(time.getMinutes() - 30);
        rcend = time.toISOString();

        //API:RecentChanges
        api.get({
            format: 'json',
            action: 'query',
            list: 'recentchanges',
            rcprop: 'user',
            rcstart: rcstart,
            rcend: rcend,
            rcshow: '!bot|!anon',
            rclimit: 500
        }).done(function(data) {
            $.each(data.query.recentchanges, function(i, item) {
                users[i] = item.user;
            });
            api.get({
                format: 'json',
                action: 'query',
                list: 'logevents',
                leprop: 'user',
                lestart: rcstart,
                leend: rcend,
                lelimit: 500
            }).done(function(data) {
                $.each(data.query.logevents, function(i, item) {
                    usersExt[i] = item.user;
                });

                Array.prototype.push.apply(users, usersExt);

                // 使用者名稱去重與分割
                users = $.unique(users.sort());

                var promises = [];
                var mark = function(data) {
                    $.each(data.query.users, function(i, user) {
                        // 找到管理员,去除adminbot
                        if ($.inArray('bot', user.groups) === -1) {
                            if ($.inArray('sysop', user.groups) > -1) {
                                admins[i] = user.name;
                            }
                            if ($.inArray('rollbacker', user.groups) > -1) {
                                rollbackers[i] = user.name;
                            }
                            if ($.inArray('patroller', user.groups) > -1) {
                                patrollers[i] = user.name;
                            }
                        }
                    });
                };
                for (var i=0; i<(users.length+50)/50; i++) {
                    promises.push(api.get({
                        format: 'json',
                        action: 'query',
                        list: 'users',
                        ususers: users.slice(i*50, (i+1)*50).join('|'),
                        usprop: 'groups'
                    }).done(mark));
                }

                // 查询用户权限
                $.when.apply($, promises).done(function () {
                    // 消除空值
                    var filter = function(n) {
                        return n;
                    };
                    
                    admins = admins.filter(filter);
                    rollbackers = rollbackers.filter(filter);
                    patrollers = patrollers.filter(filter);

                    var userlink = function(user) {
                        var user2 = user.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&lt;');
                        return '<a href="/wiki/User:' + user2 + '" target="_blank">' + user2 + '</a>&nbsp;<small style="opacity:.75;">(<a href="/wiki/User talk:' + user2 + '" target="_blank">留言</a>)</small> ';
                    }
                    
                    if (admins.length + rollbackers.length + patrollers.length > 0) {
                        var adminsstring = ['<p>現在並不能搞破壞,因為……</p>'];

                        if (admins.length > 0) {
                            adminsstring.push('<p style="word-break:break-all;">有' + admins.length + '個管理員在看著你:');
                            $.each(admins, function(i, e) {
                                adminsstring.push(userlink(e));
                            });
                            adminsstring.push('</p>');
                        }

                        if (patrollers.length > 0) {
                            adminsstring.push('<p style="word-break:break-all;">有' + patrollers.length + '個巡查員在等著你:');
                            $.each(patrollers, function(i, e) {
                                adminsstring.push(userlink(e));
                            });
                            adminsstring.push('</p>');
                        }

                        if (rollbackers.length > 0) {
                            adminsstring.push('<p style="word-break:break-all;">有' + rollbackers.length + '個回退員準備抓你立功:');
                            $.each(rollbackers, function(i, e) {
                                adminsstring.push(userlink(e));
                            });
                            adminsstring.push('</p>');
                        }

                        mw.notify($(adminsstring.join('')));
                    } else {
                        mw.notify('現在沒有管理人員在線,可以去搞破壞了!');
                    }
                }).fail(function () {
                    mw.notify('查詢時出現錯誤');
                });
            });
        });
    });
})(jQuery, mw);