跳转到内容

User:小躍/LiveClock.js

维基百科,自由的百科全书
注意:保存之后,你必须清除浏览器缓存才能看到做出的更改。Google ChromeFirefoxMicrosoft EdgeSafari:按住⇧ Shift键并单击工具栏的“刷新”按钮。参阅Help:绕过浏览器缓存以获取更多帮助。
/*
Source: https://www.mediawiki.org/wiki/MediaWiki:Gadget-UTCLiveClock.js
Using files:
https://upload.wikimedia.org/wikipedia/commons/thumb/4/4b/Dígito_c0.svg/15px-Dígito_c0.svg.png
https://upload.wikimedia.org/wikipedia/commons/thumb/8/87/Dígito_c1.svg/15px-Dígito_c1.svg.png
https://upload.wikimedia.org/wikipedia/commons/thumb/b/b0/Dígito_c2.svg/15px-Dígito_c2.svg.png
https://upload.wikimedia.org/wikipedia/commons/thumb/d/d0/Dígito_c3.svg/15px-Dígito_c3.svg.png
https://upload.wikimedia.org/wikipedia/commons/thumb/9/94/Dígito_c4.svg/15px-Dígito_c4.svg.png
https://upload.wikimedia.org/wikipedia/commons/thumb/5/5c/Dígito_c5.svg/15px-Dígito_c5.svg.png
https://upload.wikimedia.org/wikipedia/commons/thumb/1/1e/Dígito_c6.svg/15px-Dígito_c6.svg.png
https://upload.wikimedia.org/wikipedia/commons/thumb/5/5c/Dígito_c7.svg/15px-Dígito_c7.svg.png
https://upload.wikimedia.org/wikipedia/commons/thumb/0/0c/Dígito_c8.svg/15px-Dígito_c8.svg.png
https://upload.wikimedia.org/wikipedia/commons/thumb/8/8a/Dígito_c9.svg/15px-Dígito_c9.svg.png
修改者:小躍
*/
mw.loader.using( ['mediawiki.util', 'mediawiki.api'] ).then( function () {

function showTime( $target ) {
	var now = new Date();
	var commonlink='//upload.wikimedia.org/wikipedia/commons/thumb/';
	var imglink=['4/4b','8/87','b/b0','d/d0','9/94','5/5c','1/1e','5/5c','0/0c','8/8a'];
			
	// Set the time.
	var ;
	var 時差=parseInt(mw.user.options.get('timecorrection').split('|')[1])/60;//用戶偏好設定
	if(時差>0){
		 = ((now.getUTCHours()+時差)>23)?(now.getUTCHours()+時差)-24:now.getUTCHours() + 時差;	
	}
	else if(時差<0){
		 = ((now.getUTCHours()+時差)<0)?(now.getUTCHours()+時差)+24:now.getUTCHours() + 時差;	
	}else{
		 = now.getUTCHours();	
	}
	var  = now.getUTCMinutes();
	var  = now.getUTCSeconds();
		var 時間冒號='<img src="'+commonlink+'/4/47/Dígito_cS.svg/8px-Dígito_cS.svg.png">';
		var 顯秒=
		'<img src="'+commonlink+imglink[(Math.floor(/10))]+
			'/Dígito_c'+Math.floor(/10).toString()+
			'.svg/12px-Dígito_c'+Math.floor(/10).toString()+'.svg.png">'+
		'<img src="'+commonlink+imglink[(%10)]+'/Dígito_c'+(%10).toString()+
			'.svg/12px-Dígito_c'+(%10).toString()+'.svg.png">';
		var 顯分=
		'<img src="'+commonlink+imglink[(Math.floor(/10))]+
			'/Dígito_c'+Math.floor(/10).toString()+
			'.svg/12px-Dígito_c'+Math.floor(/10).toString()+'.svg.png">'+
		'<img src="'+commonlink+imglink[(%10)]+'/Dígito_c'+(%10).toString()+
			'.svg/12px-Dígito_c'+(%10).toString()+'.svg.png">';
		var 顯時=
		'<img src="'+commonlink+imglink[(Math.floor(/10))]+
			'/Dígito_c'+Math.floor(/10).toString()+
			'.svg/12px-Dígito_c'+Math.floor(/10).toString()+'.svg.png">'+
		'<img src="'+commonlink+imglink[(%10)]+'/Dígito_c'+(%10).toString()+
			'.svg/12px-Dígito_c'+(%10).toString()+'.svg.png">';
	$target.html(顯時+時間冒號+顯分+時間冒號+顯秒 );

	// Schedule the next time change.
	// 
	// We schedule the change for 100 ms _after_ the next clock tick. The delay
	// from setTimeout is not precise, and if we aim exactly for the tick, there
	// is a chance that the function will run slightly before it. If this
	// happens, we will display the same time for two seconds in a row - not
	// good. By scheduling 100 ms after the tick, we will always be about 100 ms
	// late, but we are also very likely to display a new time every second.
	var ms = now.getUTCMilliseconds();
	setTimeout( function () {
		showTime( $target );
	}, 1100 - ms );
}

function liveClock() {
	// Set CSS styles. We do this here instead of on the CSS page because some
	// wikis load this page directly, without loading the accompanying CSS.
	mw.util.addCSS( '#utcdate a { font-weight:bolder; font-size:120%; }' );

	// Reset whitespace that was set in the peer CSS gadget; this prevents the
	// effect of the p-personal menu jumping to the left when the JavaScript
	// loads.
	$( '.client-js > body.skin-vector #p-personal ul' ).css( 'margin-right', 'initial' );
	$( '.client-js > body.skin-monobook #p-personal ul' ).css( 'margin-right', 'initial' );


	// We are experimenting with different locations for this in Vector 2022 based on feedback
	// so currently this is limited to MediaWiki.org
	var dbName = mw.config.get( 'wgDBname' );
	var showOutsideDropdown = dbName === 'mediawikiwiki' || dbName === 'enwiki' || dbName === 'zhwiki';
	var clockLocation = document.getElementById( 'p-vector-user-menu-overflow' ) && showOutsideDropdown ?
		'p-vector-user-menu-overflow' : 'p-personal';
	addLiveClockInstance( clockLocation );
	addLiveClockInstance( 'p-personal-sticky-header' );
}

function addLiveClockInstance( clockLocation ) {
	// Add the portlet link.
	var node = mw.util.addPortletLink(
		clockLocation,
		mw.util.getUrl( null, { action: 'purge' } ),
		'',
		'utcdate'
	);
	if ( !node ) {
		return;
	}

	// Purge the page when the clock is clicked. We have to do this through the
	// API, as purge URLs now make people click through a confirmation screen.
	$( 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();
	} );

	// Show the clock.
	showTime( $( node ).find( 'a' ).first() );
}

$( liveClock );
} );