User:Wetitpig0/OnlineOffline.js
外观
注意:保存之后,你必须清除浏览器缓存才能看到做出的更改。Google Chrome、Firefox、Microsoft Edge及Safari:按住⇧ Shift键并单击工具栏的“刷新”按钮。参阅Help:绕过浏览器缓存以获取更多帮助。
(function($, mw) {
// 只在用户或用户讨论名字空间显示
var ns = mw.config.get('wgNamespaceNumber');
if ( ns === 2 || ns === 3 ) {
var api = new mw.Api();
var username = mw.config.get('wgTitle').split('/')[0];
var msg = '';
// 需要元素
var div = document.getElementById('content').insertBefore(document.createElement('div'), document.getElementById('bodyContent'));
div.id = 'mw-status';
div.align = 'right';
div.appendChild(document.createTextNode('在綫情況:'));
var img = div.appendChild(document.createElement('img'));
// 現在時間
var current = new Date();
// API:RecentChanges
api.get({
format: 'json',
action: 'query',
list: 'recentchanges',
rcuser: username,
rcprop: 'timestamp',
rclimit: 1
}).done(function(data1) {
// API:LogEvents
api.get({
format: 'json',
action: 'query',
list: 'logevents',
leuser: username,
leprop: 'timestamp|type',
lelimit: 1
}).done(function(data2) {
var string = "上綫時間:";
img.src = "/media/wikipedia/commons";
// 從未在綫IP用戶
if ( !(data1.query.recentchanges.length + data2.query.logevents.length) ) {
img.src += "/2/27/Disc_Plain_black.svg";
msg = "從未在綫 (IP用戶)";
}
// 只創建賬號的用戶
else if ( data2.query.logevents.length == 1 && data2.query.logevents[0].type == 'newusers' && ( data2.query.logevents[0].action == 'create' || data2.query.logevents[0].action == 'autocreate' ) && data1.query.recentchanges.length < 2 ) {
img.src += "/2/27/Disc_Plain_black.svg";
msg = "未曾上綫的新用戶";
}
else {
if ( !data2.query.logevents.length ) {
data2.query.logevents[0] = {"timestamp": 1};
}
if ( !data1.query.recentchanges.length ) {
data1.query.recentchanges[0] = {"timestamp": 1};
}
// 比較大小
var hist = ( new Date(data1.query.recentchanges[0].timestamp) > new Date(data2.query.logevents[0].timestamp) ? new Date(data1.query.recentchanges[0].timestamp) : new Date(data2.query.logevents[0].timestamp) );
var diff = current.getTime() - hist.getTime();
// 最後更改時間
var date = hist.getUTCFullYear() + "-" + ( hist.getUTCMonth() + 1 > 9 ? (hist.getUTCMonth() + 1) : "0" + (hist.getUTCMonth() + 1) )+ "-" + ( hist.getUTCDate() > 9 ? hist.getUTCDate() : "0" + hist.getUTCDate() );
var time = ( hist.getUTCHours() > 9 ? hist.getUTCHours() : "0" + hist.getUTCHours() ) + ":" + ( hist.getUTCMinutes() > 9 ? hist.getUTCMinutes() : "0" + hist.getUTCMinutes() ) + ":" + ( hist.getUTCSeconds() > 9 ? hist.getUTCSeconds() : "0" + hist.getUTCSeconds() );
string += date + " " + time + ' (UTC)';
// 離綫用紅點,可能離綫用黃點,在綫用綠點,非活躍用戶用藍點
if ( diff < 300000 ) {
img.src += "/7/74/Disc_Plain_green.svg";
msg = "在綫;";
}
else if ( diff < 600000 ) {
img.src += "/7/75/Disc_Plain_yellow_dark.svg";
msg = "可能在綫;最後";
}
else if ( diff > 2592000000 ) {
img.src += "/0/02/Disc_Plain_blue.svg";
msg = "非活躍;最後";
}
else {
img.src += "/1/13/Disc_Plain_red.svg";
msg = "離綫;最後";
}
msg += string;
}
img.onclick = function (){
var p = document.createElement("p");
p.appendChild(document.createTextNode(msg + " ("));
var a = p.appendChild(document.createElement("a"));
a.href = "http://zh.wikipedia.org/wiki/User_talk:" + username;
a.target = "_blank";
a.title = "留言";
a.appendChild(document.createTextNode("留言"));
p.appendChild(document.createTextNode(")"));
mw.notify(p);
};
img.width = img.height = 25;
img.alt = msg;
});
});
}
})(jQuery, mw);