User:Emojiwiki/module/userStat.js

维基百科,自由的百科全书

注意:保存之后,你必须清除浏览器缓存才能看到做出的更改。Google ChromeFirefoxMicrosoft EdgeSafari:按住⇧ Shift键并单击工具栏的“刷新”按钮。参阅Help:绕过浏览器缓存以获取更多帮助。

/* some code from [[User:Sun00217/現在我可以搞破壊了嗎.js]] */
/* Fork from [[User:Sunny00217/userStat.js]] */
/* cover from [[User:Emojiwiki/module/userStat.js]] (old version) */
$( function () {
	if ( [ 2, 3 ].indexOf( mw.config.get( "wgNamespaceNumber" ) ) === -1 || mw.config.get( "wgAction" ) !== "view" ) {
		return;
	}

	function debug( message ) {
		if ( typeof console.log === "function" ) {
			console.log( "[userStat] " + message );
		}
	}

	var tables = window.userStat ||  {
		locked: {
			text: wgULS( "被全域锁定", "被全域鎖定" ),
			img: "//upload.wikimedia.org/wikipedia/commons/thumb/7/7f/Symbol_oppose_vote.svg/25px-Symbol_oppose_vote.svg.png"
		},
		infiniteBlocked: {
			text: wgULS( "被永久封禁", "被永久封鎖" ),
			img: "//upload.wikimedia.org/wikipedia/commons/thumb/7/7f/Symbol_oppose_vote.svg/25px-Symbol_oppose_vote.svg.png"
		},
		bot: {
			text: wgULS( "机械人", "機器人" ),
			img: "//upload.wikimedia.org/wikipedia/commons/thumb/9/9b/Symbol_star_FA_gold.svg/25px-Symbol_star_FA_gold.svg.png"
		},
		contributingBot: {
			text: wgULS( "勤力的机械人", "勤勞的機器人" ),
			img: "//upload.wikimedia.org/wikipedia/commons/thumb/9/9b/Symbol_star_FA_gold.svg/25px-Symbol_star_FA_gold.svg.png"
		},
		contributing: {
			text: wgULS( "刚刚贡献了维基百科", "剛剛貢獻了維基百科" ),
			img: "//upload.wikimedia.org/wikipedia/commons/thumb/9/94/Symbol_support_vote.svg/25px-Symbol_support_vote.svg.png"
		},
		online: {
			text: wgULS( "在线", "在線" ),
			img: "" // ?
		},
		mayOnline: {
			text: wgULS( "可能在线", "可能在線" ),
			img: "//upload.wikimedia.org/wikipedia/commons/thumb/9/94/Symbol_support_vote.svg/25px-Symbol_support_vote.svg.png"
		},
		mayOffline: {
			text: wgULS( "可能离线", "可能離線" ),
			img: "//upload.wikimedia.org/wikipedia/commons/thumb/8/85/Symbol_unrelated.svg/25px-Symbol_unrelated.svg.png"
		},
		offline: {
			text: wgULS( "离线", "離線" ),
			img: "" // ?
		},
		break: {
			text: wgULS( "休假", "休假" ),
			img: "" // ?
		},
		away: {
			text: wgULS( "長假", "長假" ),
			img: "" // ?
		},
		unknown: {
			text: wgULS( "未知", "未知" ),
			img: ""
		}
	};

	var uname = mw.config.get( "wgTitle" ).split( "/" )[ 0 ];
	debug( "detected username " + uname );

	var $ele = $( "<span>" ).attr( "id", "wmgUserStat" ).attr("class","wmgUserStat"),
		// eslint-disable-next-line no-jquery/no-global-selector
		$tab = $( "#ca-nstab-user > a" );

	if ( !$tab.length ) {
		debug( "Fail to find nstab." );
		return;
	}

	$tab.append(
		$( "<span>" ).text( " (" ),
		$ele,
		$( "<span>" ).text( ")" )
	);

	new mw.Api().get( {
		action: "query",
		list: [ "recentchanges", "users", "globalallusers" ],
		usprop: [ "blockinfo", "groups" ],
		ususers: uname,
		rcuser: uname,
		agufrom: uname,
		aguto: uname,
		agulimit: 1,
		aguprop: "lockinfo",
		rclimit: 1
	} ).done( function ( data ) {
		if ( "errors" in data ) {
			debug( "Error while checking recent user change data" );
			mw.notify( wgULS(
				"查询用户最近更改资料时发生错误",
				"查詢用戶近期變更資料時發生錯誤",
				undefined, // fallback
				"查詢使用者近期變更資料時發生錯誤"
			) );
			return;
		}

		var key = "unknown";

		// eslint-disable-next-line max-len
		var editTS = +new Date( data.query.recentchanges[ 0 ] && data.query.recentchanges[ 0 ].timestamp );
		editTS = isNaN( editTS ) ? 0 : editTS;

		var tsDiff = ( +new Date() - editTS ) / 1000;

		if ( data.query.globalallusers[ 0 ].locked === "" ) {
			key = "locked";
		} else if ( data.query.users[ 0 ].blockexpiry === "infinite" ) {
			key = "infiniteBlocked";
		} else if ( data.query.users[ 0 ].groups.indexOf( "bot" ) > -1 ) {
			key = "bot";
			if ( tsDiff < 60 * 1000 ) {
				key = "contributingBot";
			}
		} else if ( tsDiff ) {
			if ( tsDiff < 60 * 1000 ) {
				key = "contributing";
			} else if ( tsDiff < 15 * 60 * 1000 ) {
				key = "online";
			} else if ( tsDiff < 30 * 60 * 1000 ) {
				key = "mayOnline";
			} else if ( tsDiff < 60 * 60 * 1000 ) {
				key = "mayOffline";
			} else {
				key = "offline";
				if ( tsDiff > 7 * 24 * 60 * 60 * 1000 ) {
					key = "break";
				} else if ( tsDiff > 30 * 2 * 24 * 60 * 60 * 1000 ) {
					key = "away";
				}
			}
		}
		debug( "Status: " + key );

		$(".wmgUserStat, .emoji_userStat").html($.extend(
			$( tables[ key ].img ? "<img>" : "" )
				.attr( "src", tables[ key ].img )
				.attr( "title", tables[ key ].text ),
			$( "<span>" )
				.text( tables[ key ].text )
		));
	} );
}() );