跳转到内容

User:Wetitpig0/OnlineOffline.js

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

这是本页的一个历史版本,由Wetitpig0留言 | 贡献2017年9月27日 (三) 11:30编辑。这可能和当前版本存在着巨大的差异。

注意:保存之后,你必须清除浏览器缓存才能看到做出的更改。Google ChromeFirefoxMicrosoft EdgeSafari:按住⇧ Shift键并单击工具栏的“刷新”按钮。参阅Help:绕过浏览器缓存以获取更多帮助。
(function($, mw) {
    // 只在用户或用户讨论名字空间显示
    var ns = mw.config.get('wgNamespaceNumber');
    var title = mw.config.get('wgTitle');
    
    if ( ns === 2 || ns === 3 || ( ns === -1 && title.search("/")[1] > -1 ) ) {
    	var api = new mw.Api();
    	var username = ( ns === -1 ? title.split('/')[1] : title.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();
        var showtime = false;

        // API:RecentChanges
        api.get({
            format: 'json',
            action: 'query',
            list: 'usercontribs',
            ucuser: username,
            ucprop: 'timestamp',
            uclimit: 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.usercontribs.length + data2.query.logevents.length) ) {
            		img.src += "/2/27/Disc_Plain_black.svg";
            		msg = "從未在綫 (IP用戶)";
            	}
            	// 只創建賬號的用戶
            	else if ( !data1.query.usercontribs.length ) {
            		img.src += "/2/27/Disc_Plain_black.svg";
            		msg = "未曾上綫的新用戶";
            	}
            	else {
            		showtime = true;
            		
            		if ( !data2.query.logevents.length ) {
            			data2.query.logevents[0] = {"timestamp": 1};
            		}
            		if ( !data1.query.usercontribs.length ) {
            			data1.query.recentchanges[0] = {"timestamp": 1};
            		}
            		// 比較大小
                	var hist = ( new Date(data1.query.usercontribs[0].timestamp) > new Date(data2.query.logevents[0].timestamp) ? new Date(data1.query.usercontribs[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 = "可能在綫;";
                	    string = "最後" + string;
                	}
                	else if ( diff > 2592000000 ) {
                		img.src += "/0/02/Disc_Plain_blue.svg";
                		msg = "非活躍;";
                		string = "最後" + string;
                	}
                	else {
                		img.src += "/1/13/Disc_Plain_red.svg";
                	    msg = "離綫;";
                	    string = "最後" + string;
                	}
            	}
            	
            	var p = document.createElement("p");
            	
                p.appendChild(document.createTextNode(msg));
                
                if ( showtime ) {
                	p.appendChild(document.createElement("br"));
                	p.appendChild(document.createTextNode(string));
                }
                
                p.appendChild(document.createElement("br"));
                p.appendChild(document.createTextNode("("));
                
                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(")"));
            	
                img.onclick = function () {
                	mw.notify(p);
                }; 
                img.width = img.height = 25;
                img.alt = msg;
            });
        });
    }
})(jQuery, mw);