User:ZhaoFJx/conline.js
注意:保存之后,你必须清除浏览器缓存才能看到做出的更改。Google Chrome、Firefox、Microsoft Edge及Safari:按住⇧ Shift键并单击工具栏的“刷新”按钮。参阅Help:绕过浏览器缓存以获取更多帮助。
// 改自[[User:Wetitpig0/有誰在啊.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/w/index.php?title=User:' + user + '" target="_blank">' + user + '</a><small style="opacity:.75;">(<a href="http://zh.wikipedia.org/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);