利用者:Atmark-chan/common.js/utcClock.js
表示
お知らせ: 保存した後、ブラウザのキャッシュをクリアしてページを再読み込みする必要があります。
多くの Windows や Linux のブラウザ
- Ctrl を押しながら F5 を押す。
Mac における Safari
Mac における Chrome や Firefox
- ⌘ Cmd と ⇧ Shift を押しながら R を押す。
詳細についてはWikipedia:キャッシュを消すをご覧ください。
//https://www.mediawiki.org/wiki/MediaWiki:Gadget-UTCLiveClock.js を参考に一部改変
mw.loader.using( ['mediawiki.util', 'mediawiki.api'] ).then( function () {
//2桁に文字列操作
function padWithZeroes( num ) {
// Pad a number with zeroes. The number must be an integer where
// 0 <= num < 100.
return num < 10 ? '0' + num.toString() : num.toString();
}
//文字列設定
function showTime( $target ) {
var now = new Date();
// Set the time.
var d = ''; //日
var hh = now.getUTCHours(); //UTC時
var hh2 = now.getUTCHours(); //JST時
var mm = now.getUTCMinutes(); //分
var ss = now.getUTCSeconds(); //秒
if ( hh2 >= 15 ) {
hh2 = hh2 - 15;
d = '<small>前</small> ';
} else {
hh2 = hh2 + 9;
}
$target.html( 'UTC: ' + d + padWithZeroes( hh ) + ':' + padWithZeroes( mm ) + ':' + padWithZeroes( ss ));
// 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( '#pt-utcclock a { font-weight:bolder; }' );
// 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' );
// Add the portlet link.
var node = mw.util.addPortletLink(
'p-personal',
mw.util.getUrl( null, { action: 'purge' } ),
'',
'pt-utcclock',
null,
null,
'#pt-logout'
);
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 );
} );