跳转到内容

User:Jpcomic/onlineURcheck.js

维基百科,自由的百科全书
注意:保存之后,你必须清除浏览器缓存才能看到做出的更改。Google ChromeFirefoxMicrosoft EdgeSafari:按住⇧ Shift键并单击工具栏的“刷新”按钮。参阅Help:绕过浏览器缓存以获取更多帮助。
// 引用Carrotkit的技術所衍生的作品。
// 目前在一些Special页面无法使用
// Create portlet link
var portletLinkOnline = mw.util.addPortletLink(
    'p-personal',
    '#',
    '在线管理人员',
    't-onlineadmin',
    '点击查看当前在线管理员、巡查员、回退员',
    '',
    '#pt-userpage'
);

var rcstart, rcend, time;
var users = {};
var admins = [], rollbackers = [], patrollers = [];
var api = new mw.Api();
var queue1 = [], queue2 = [];
var i = 0, k = 0;
var user;

// Bind click handler
$(portletLinkOnline).click(function(e) {
	var t0 = performance.now();
	e.preventDefault();

	users = {};
	admins = [];
	rollbackers = [];
	patrollers = [];
	queue1 = [], queue2 = [];
	i = 0, k = 0;

	// 最近更改5分钟内的编辑用户
	time = new Date();
	rcstart = time.toISOString();
	time.setMinutes(time.getMinutes() - 5);
	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[item.user] = true;
		});
		$.each(data.query.logevents, function(i, item) {
			users[item.user] = true;
		});

		var filter = function(n) {
			return n;
		};

		for (user in users) {
			queue1[i] = user;
			i++;
			if (i === 50) {
				queue1 = queue1.filter(filter);
				i = queue1.length;
				if (i === 50) {
					queue2[k] = queue1;
					queue1 = [];
					i = 0;
					k++;
				}
			}
		}
		if (queue1.length > 0) {
			queue1 = queue1.filter(filter);
			queue2[k] = queue1;
			k++;
		}

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

		// 查询用户权限
		$.when.apply($, promises).done(function() {

			admins = admins.filter(filter);
			rollbackers = rollbackers.filter(filter);
			patrollers = patrollers.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 > 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);
				}

				mw.notify(parent);
			} else {
				mw.notify('当前没任何管理人员在线');
			}
		}).fail(function() {
			mw.notify('查询失败');
		});
	});
	console.log(performance.now() - t0);
});