跳转到内容

User:Wetitpig0/有誰在啊.js:修订间差异

维基百科,自由的百科全书
删除的内容 添加的内容
无编辑摘要
无编辑摘要
第56行: 第56行:
users[i] = item.user;
users[i] = item.user;
});
});
var j = users.length;
$.each(data.query.logevents, function(i, item) {
$.each(data.query.logevents, function(i, item) {
users[j + i] = item.user;
users[j + i] = item.user;

2018年6月4日 (一) 06:48的版本

/**
 * 取自[[User:Psyduck buab/現在我可以搞破壊了嗎.js]]
 */

(function($, mw) {
    // Create portlet link
    var portletLinkOnline = mw.util.addPortletLink(
        'p-help',
        '#',
        '有邊個睇住呀?',
        't-spy',
        '找行政人員',
        'p',
        '#n-portal'
    );

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

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

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

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

        //API:RecentChanges
        api.get({
            format: 'json',
            action: 'query',
            list: 'recentchanges|logevents',
            rcprop: 'user',
            rcstart: rcstart,
            rcend: rcend,
            rcshow: '!bot|!anon',
            rclimit: 500,
            leprop: 'user',
            lestart: rcstart,
            leend: rcend,
            lelimit: 500
        }).done(function(data) {
            $.each(data.query.recentchanges, function(i, item) {
                users[i] = item.user;
            });
            var j = users.length;
            $.each(data.query.logevents, function(i, item) {
                users[j + i] = item.user;
            });
            // 使用者名稱去重與分割
            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;
                        }
                        if ($.inArray('bureaucrat', user.groups) > -1) {
                      	    bureaucrats[i] = user.name; 
                        }
                        if ($.inArray('checkuser', user.groups) > -1) {
                            checkusers[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);
                bureaucrats = bureaucrats.filter(filter); 
                checkusers = checkusers.filter(filter);

                var userlink = function(user) {
                    var p = document.createElement("p");
                    p.innerHTML = '<a href="http://zh.wikipedia.org/zhwiki/w/index.php?title=User:' + user + '" target="_blank">' + user + '</a><small style="opacity:.75;">(<a href="http://zh.wikipedia.org/zhwiki/w/index.php?title=User_talk:' + user + '" target="_blank">留言</a>)</small></p>';
                    return p;
                }; 
                    
                if (admins.length + rollbackers.length + patrollers.length + bureaucrats.length + checkusers.length > 0) {
                    var parent = document.createElement("p");
                    var start = parent.appendChild(document.createElement("p"));
                    start.appendChild(document.createTextNode("依家唔可以搞破壞,因為……"));

                    if (admins.length > 0) {
                        var adminp = document.createElement("p");
                        adminp.style = "word-break:break-all;";
                        adminp.innerHTML = "有" + admins.length + "個管理員望住你,問你死未:";
                        $.each(admins, function(i, e) {
                            adminp.appendChild(userlink(e));
                        });
                        parent.appendChild(adminp);
                    }

                    if (patrollers.length > 0) {
                        var patrolp = document.createElement("p");
                        patrolp.style = "word-break:break-all;";
                        patrolp.innerHTML = "有" + patrollers.length + "個巡查員等緊你,問你死未:";
                        $.each(patrollers, function(i, e) {
                            patrolp.appendChild(userlink(e));
                        });
                        parent.appendChild(patrolp);
                    }

                    if (rollbackers.length > 0) {
                        var rollbackp = document.createElement("p");
                        rollbackp.style = "word-break:break-all;";
                        rollbackp.innerHTML = "有" + rollbackers.length + "個回退員準備捉你立功,問你死未:";
                        $.each(rollbackers, function(i, e) {
                            rollbackp.appendChild(userlink(e));
                        });
                        parent.appendChild(rollbackp);
                    }
                    
                    if (bureaucrats.length > 0) {
                       	var bureaup = document.createElement("p");
                       	bureaup.style = "word-break:break-all;";
                       	bureaup.innerHTML = "有" + bureaucrats.length + "個行政員盯實你,問你死未:";
                        $.each(bureaucrats, function(i, e) {
                            bureaup.appendChild(userlink(e));
                        });
                        parent.appendChild(bureaup);
                    }
                    
                    if (checkusers.length > 0) {
                        var checkp = document.createElement("p");
                        checkp.style = "word-break:break-all;";
                        checkp.innerHTML = "有" + checkusers.length + "個用戶查核員想查你,問你死未:";
                        $.each(checkusers, function(i, e) {
                            checkp.appendChild(userlink(e));
                        });
                        parent.appendChild(checkp);
                    }

                    mw.notify(parent);
                } else {
                    mw.notify('現在沒有管理人員在線,可以去搞破壞啦,快少少啦!');
                }
            }).fail(function () {
                mw.notify('查詢失敗');
            });
        });
    });
})(jQuery, mw);