跳转到内容

User:Wetitpig0/OnlineOffline.js

维基百科,自由的百科全书
注意:保存之后,你必须清除浏览器缓存才能看到做出的更改。Google ChromeFirefoxMicrosoft EdgeSafari:按住⇧ Shift键并单击工具栏的“刷新”按钮。参阅Help:绕过浏览器缓存以获取更多帮助。
if (typeof window.wgUXS === 'undefined') {
	window.wgUXS = function (wg, hans, hant, cn, tw, hk, sg, zh, mo, my) {
		var ret = {
			'zh': zh || hans || hant || cn || tw || hk || sg || mo || my,
			'zh-hans': hans || cn || sg || my,
			'zh-hant': hant || tw || hk || mo,
			'zh-cn': cn || hans || sg || my,
			'zh-sg': sg || hans || cn || my,
			'zh-tw': tw || hant || hk || mo,
			'zh-hk': hk || ahant || mo || tw,
			'zh-mo': mo || hant || hk || tw
		};
		return ret[wg] || zh || hans || hant || cn || tw || hk || sg || mo || my; //保證每一語言有值
	};
}

if (typeof window.wgULS === 'undefined') {
	window.wgULS = function (hans, hant, cn, tw, hk, sg, zh, mo, my) {
		return wgUXS(mw.config.get('wgUserLanguage'), hans, hant, cn, tw, hk, sg, zh, mo, my);
	};
}

(function (mw) {
	// 只在用户或用户讨论名字空间显示
	var username = mw.config.get('wgRelevantUserName');

	if (username) {
		var msg = '';
		var string;

		var string1 = wgULS("时间:", "時間:");
		var string2 = wgULS("在线", "在綫", undefined, "在線");
		var string3 = wgULS("上线", "上綫", undefined, "上線");
		var string4 = wgULS("最后", "最後");

		// 需要元素
		var div = document.getElementById('content').insertBefore(document.createElement('div'), document.getElementById('bodyContent'));
		div.style.display = "none";

		div.id = 'mw-status';
		div.align = 'right';
		div.appendChild(document.createTextNode(string2 + wgULS('情况:', '情況:')));

		var img = div.appendChild(document.createElement('img'));
		img.src = "https://upload.wikimedia.org/wikipedia/commons/";

		var showtime = false;

		var api = new mw.Api();
		// API:RecentChanges, API:LogEvents, API:Blocks
		api.get({
			format: 'json',
			action: 'query',
			list: 'usercontribs|logevents|blocks',
			ucuser: username,
			leuser: username,
			bkusers: username,
			ucprop: 'timestamp',
			leprop: 'timestamp',
			uclimit: 1,
			lelimit: 1,
			bklimit: 1
		}).done(function (data) {
			var time = data.query;

			// 被封禁
			if (time.blocks.length === 1) {
				// 被永久封禁
				if (time.blocks[0].expiry === "infinity") {
					img.src += "5/50/Less_Icon_C.svg";
					msg = "已被永久封禁";
				}

				// 被暫時封禁
				else {
					img.src += "f/f5/No_Cross.svg";
					msg = "已被封禁;";

					string = "解封" + string1;
					string += time.blocks[0].expiry.replace("T", " ").replace("Z", "");
					showtime = true;
				}
			}

			// 未被封禁
			else {
				// 從未在綫IP用戶
				if ((time.usercontribs.length + time.logevents.length) === 0) {
					img.src += "2/27/Disc_Plain_black.svg";
					msg = wgULS("从未" + string2 + " (IP用户)", "從未" + string2 + " (IP用戶)");
				}
				// 只創建賬號的用戶
				else if (time.usercontribs.length === 0) {
					img.src += "2/27/Disc_Plain_black.svg";
					msg = wgULS("未曾" + string3 + "的新用户", "未曾" + string3 + "的新用戶");
				} else {
					string = string3 + string1;
					showtime = true;

					if (time.logevents.length === 0) {
						time.logevents[0] = {
							"timestamp": 1
						};
					}

					// 比較大小
					var hist = (new Date(time.usercontribs[0].timestamp) > new Date(time.logevents[0].timestamp) ? time.usercontribs[0].timestamp : time.logevents[0].timestamp);
					var diff = (new Date().getTime()) - (new Date(hist).getTime());

					// 最後更改時間
					string += hist.replace("T", " ").replace("Z", "");

					// 離綫用紅點,可能離綫用黃點,在綫用綠點,非活躍用戶用藍點
					if (diff < 300000) {
						img.src += "7/74/Disc_Plain_green.svg";
						msg = string2 + ";";
					} else if (diff < 600000) {
						img.src += "7/75/Disc_Plain_yellow_dark.svg";
						msg = wgULS("可能" + string2, "可能" + string2) + ";";
						string = string4 + string;
					} else if (diff > 2592000000) {
						img.src += "0/02/Disc_Plain_blue.svg";
						msg = wgULS("非活跃;", "非活躍;");
						string = string4 + string;
					} else {
						img.src += "1/13/Disc_Plain_red.svg";
						msg = wgULS("离线;", "離綫;", undefined, "離線;");
						string = string4 + string;
					}
				}
			}

			var p = document.createElement("p");

			p.innerHTML = msg;

			if (showtime)
				p.innerHTML += "<br/>" + string + " (UTC)";

			p.innerHTML += "<br/>(<a href='http://wiki.zwnes.eu.org/w/index.php?title=User_talk:" + username + "' target='_blank' title='留言'>留言</a>)";

			img.onclick = function () {
				mw.notify(p);
			};
			img.width = img.height = 25;
			img.alt = msg;

			div.style.display = "block";
		});
	}
})(mw);