2023年政策修订增补工作正在进行中,欢迎参与!
  • Moegirl.ICU:萌娘百科流亡社群 581077156(QQ),欢迎对萌娘百科运营感到失望的编辑者加入
  • Moegirl.ICU:账号认领正在试运行,有意者请参照账号认领流程

User:实验性:无用论废人/OnlineAdmins.js

萌娘百科,万物皆可萌的百科全书!转载请标注来源页面的网页链接,并声明引自萌娘百科。内容不可商用。
跳转到导航 跳转到搜索

注意:在保存之后,您可能需要清除浏览器缓存才能看到所作出的变更的影响。

  • Firefox/Safari:按住“Shift”的同时单击“刷新”,或按“Ctrl-F5”或“Ctrl-R”(Mac为“⌘-R”)
  • Google Chrome:按“Ctrl-Shift-R”(Mac为“⌘-Shift-R”)
  • Internet Explorer:按住“Ctrl”的同时单击“刷新”,或按“Ctrl-F5”
  • Opera:在“工具→首选项”中清除缓存
如果您已登录但该页面出现未登录状态,请尝试在地址栏的地址最后添加代码?_=1来访问最新页面。
添加代码后的本页地址如下:-{R|https://moegirl.icu/User:%E5%AE%9E%E9%AA%8C%E6%80%A7%EF%BC%9A%E6%97%A0%E7%94%A8%E8%AE%BA%E5%BA%9F%E4%BA%BA/OnlineAdmins.js?_=1}-
//将[[User:星海子/js/OnlineAdmins.js]]改造
/**
 * 取自[[zhwp:MediaWiki:Gadget-OnlineAdmins.js]],oldid=64572008。
 * 已萌百化。
 */

(function($, mw) {
    $.when($.ready, mw.loader.using(['mediawiki.api','ext.gadget.site-lib'])).then(function(){
        // Create portlet link
        var portletLinkOnline = mw.util.addPortletLink(
                'p-cactions',
                '#',
                wgULS('在线人员', '線上人員'),
                'ca-onlineadmin'
                );

        var rcstart,
            rcend,
            time;
        var users = [];
        var sysops = [],
            patrollers = [],
            goodeditors = [],
            interfaces = [],
            scripteditors = [],
            techeditors = [];
        var api = new mw.Api();

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

            users = [];
            var usersExt = [];
            sysops = [];
            patrollers = [];
            goodeditors = [];
            interfaces = [];
            scripteditors = [];
            techeditors = [];

            // 最近更改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) {
                            // 找到管理员,去除bot
                            if ($.inArray('bot', user.groups) === -1) {
                                if ($.inArray('sysop', user.groups) > -1) {
                                    sysops[i] = user.name;
                                }
                                if ($.inArray('patroller', user.groups) > -1) {
                                    patrollers[i] = user.name;
                                }
                                if ($.inArray('goodeditor', user.groups) > -1) {
                                    goodeditors[i] = user.name;
                                }
                                if ($.inArray('interface-admin', user.groups) > -1) {
                                    interfaces[i] = user.name;
                                }
                                if ($.inArray('scripteditor', user.groups) > -1) {
                                    scripteditors[i] = user.name;
                                }
                                if ($.inArray('techeditor', user.groups) > -1) {
                                    techeditors[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;
                        };

                        sysops = sysops.filter(filter);
                        patrollers = patrollers.filter(filter);
                        goodeditors = goodeditors.filter(filter);
                        interfaces = interfaces.filter(filter);
                        scripteditors = scripteditors.filter(filter);
                        techeditors = techeditors.filter(filter);

                        var userlink = function(user) {
                            var user2 = user.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&lt;');
                            return '<a href="/User:' + user2 + '" target="_blank">' + user2 + '</a>&nbsp;<small style="opacity:.75;">(<a href="/User talk:' + user2 + '" target="_blank">留言</a>)</small> ';
                        };

                        if (sysops.length + patrollers.length > 0) {
                            var adminsstring = [wgULS('<p>下面是最近30分钟之内在线的人员</p>', '<p>下面是最近30分鐘內的線上人員</p>')];

                            if (sysops.length > 0) {
                                adminsstring.push('<p style="word-break:break-all;">' + wgULS('管理员', '管理員') + ' (' + sysops.length + wgULS('个在线', '個在線') + '):');
                                $.each(sysops, function(i, e) {
                                    adminsstring.push(userlink(e));
                                });
                                adminsstring.push('</p>');
                            }

                            if (patrollers.length > 0) {
                                adminsstring.push('<p style="word-break:break-all;">' + wgULS('巡查姬', '巡查姬') + ' (' + patrollers.length + wgULS('个在线', '個在線') + '):');
                                $.each(patrollers, function(i, e) {
                                    adminsstring.push(userlink(e));
                                });
                                adminsstring.push('</p>');
                            }

                            if (goodeditors.length > 0) {
                                adminsstring.push('<p style="word-break:break-all;">' + wgULS('优质编辑者', '優質編輯者') + ' (' + goodeditors.length + wgULS('个在线', '個在線') + '):');
                                $.each(goodeditors, function(i, e) {
                                    adminsstring.push(userlink(e));
                                });
                                adminsstring.push('</p>');
                            }

                            if (interfaces.length > 0) {
                                adminsstring.push('<p style="word-break:break-all;">' + wgULS('界面管理员', '界面管理員') + ' (' + interfaces.length + wgULS('个在线', '個在線') + '):');
                                $.each(interfaces, function(i, e) {
                                    adminsstring.push(userlink(e));
                                });
                                adminsstring.push('</p>');
                            }

                            if (scripteditors.length > 0) {
                                adminsstring.push('<p style="word-break:break-all;">' + wgULS('脚本编辑员', '腳本編輯員') + ' (' + scripteditors.length + wgULS('个在线', '個在線') + '):');
                                $.each(scripteditors, function(i, e) {
                                    adminsstring.push(userlink(e));
                                });
                                adminsstring.push('</p>');
                            }

                            if (techeditors.length > 0) {
                                adminsstring.push('<p style="word-break:break-all;">' + wgULS('技术编辑员', '技術編輯員') + ' (' + techeditors.length + wgULS('个在线', '個在線') + '):');
                                $.each(techeditors, function(i, e) {
                                    adminsstring.push(userlink(e));
                                });
                                adminsstring.push('</p>');
                            }

                            mw.notify($(adminsstring.join('')));
                        } else {
                            mw.notify(wgULS('目前没有人员在线。', '目前沒有人員在線。'));
                        }
                    }).fail(function() {
                        mw.notify(wgULS('查询时发生错误,请稍后重试。', '查詢時發生錯誤,請稍後重試。'));
                    });
                });
            });
        });
    });
})(jQuery, mw);