跳转到内容

User:Wetitpig0/有誰在啊.js

维基百科,自由的百科全书
注意:保存之后,你必须清除浏览器缓存才能看到做出的更改。Google ChromeFirefoxMicrosoft EdgeSafari:按住⇧ Shift键并单击工具栏的“刷新”按钮。参阅Help:绕过浏览器缓存以获取更多帮助。
/**
 * 取自[[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();
	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 = [];
		bureaucrats = [];
		checkusers = [];
		queue1 = [], queue2 = [];
		i = 0, k = 0;

		// 最近更改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[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;
							}
							if (user.groups.indexOf('bureaucrat') > -1) {
								bureaucrats[i] = user.name;
							}
							if (user.groups.indexOf('checkuser') > -1) {
								checkusers[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);
				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('查詢失敗');
			});
		});
		console.log(performance.now() - t0);
	});
})(jQuery, mw);