User:Wetitpig0/Clock.js

注意:保存之后,你必须清除浏览器缓存才能看到做出的更改。Google ChromeFirefoxMicrosoft EdgeSafari:按住⇧ Shift键并单击工具栏的“刷新”按钮。参阅Help:绕过浏览器缓存以获取更多帮助。
/**
 * From https://www.mediawiki.org/w/index.php?title=MediaWiki:Gadget-UTCLiveClock.js
 *
 * Usage:
 *
 *     importScript('User:Wetitpig0/Clock.js');
 *
 */
/*global mw, $, UTCLiveClockConfig:true */
mw.loader.using(['mediawiki.util', 'mediawiki.api']).then( function () {
var $target;

function showTime( $target ) {
	var now = new Date();
	var yyyy = now.getUTCFullYear();
	var mm = now.getUTCMonth() + 1;
	var dd = now.getUTCDate();
	var hh = now.getUTCHours();
	var ii = now.getUTCMinutes();
	var ss = now.getUTCSeconds();
	var diff = now.getTimezoneOffset() / -60; 
	var diffh = Math.floor( diff );
	var diffm = ( diff - diffh ) * 60;
	var date = yyyy + '-' + ( mm < 10 ? '0' + mm : mm ) + '-' + ( dd < 10 ? '0' + dd : dd );
	var time = ( hh < 10 ? '0' + hh : hh ) + ':' + ( ii < 10 ? '0' + ii : ii ) + ':' + ( ss < 10 ? '0' + ss : ss );
	var tz = ( ( diff > 0 ? '+' : '' ) + diffh + ':' + ( diffm < 10 ? '0' + diffm : diffm ) );
	$target.html( date + ' ' + time + ' ' + tz );

	var ms = now.getUTCMilliseconds();

	setTimeout( function () {
		showTime( $target );
	}, 1100 - ms );
}

function liveClock() {
	mw.util.addCSS( '#n-utcdate a { font-weight:bolder; font-size:100%; }' );

	if ( !window.UTCLiveClockConfig ) {
		UTCLiveClockConfig = {};
	}
	var node = mw.util.addPortletLink(
		'p-navigation',
		mw.util.getUrl( null, { action: 'purge' } ),
		'',
		'n-utcdate',
		null,
		null,
		'#n-mainpage-description'
	);
	if ( !node ) {
		return;
	}
	$( node ).on( 'click', function ( e ) {
		new mw.Api().post( { action: 'purge', titles: mw.config.get( 'wgPageName' ) } ).then( function () {
			location.reload();
		}, function () {
			mw.notify( 'Purge failed', { type: 'error' } );
		} );
		e.preventDefault();
	} );
	$( node ).find( 'a:first' ).css("color", "#F00");
	showTime( $( node ).find( 'a:first' ) );
}

$( liveClock );
} );