//https://www.mediawiki.org/wiki/MediaWiki:Gadget-UTCLiveClock.js をもとに作成。
mw.loader.using( ['mediawiki.util', 'mediawiki.api'] ).then( function () {
function padWithZeroes( num ) {
// numが1桁の場合、頭に0をつける。
// numは 0 <= num < 100 の範囲の整数でなければならない。
return num < 10 ? '0' + num : num;
}
function showTime( $target ) {
var now = new Date();
// 時刻を作る。
var doweek = new Array ("日","月","火","水","木","金","土"); //曜日の表示文字
var y = now.getFullYear(); //年
var mo = padWithZeroes(now.getMonth()+1); //月。1月を0と返すので1を足している
var dd = padWithZeroes(now.getDate()); //日
var dw = doweek[now.getDay()]; //曜日インデックス
var hh = padWithZeroes(now.getHours()); //時
var min = padWithZeroes(now.getMinutes()); //分
var ss = padWithZeroes(now.getSeconds()); //秒
var time = y + '/' + mo + '/' + dd + ' (' +dw+ ') ' + hh + ':' + min + ':' + ss ;
$target.text( time );
// 更新間隔を調整。
setTimeout( function () {
showTime( $target );
}, 500 );
}
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' );
// Add the portlet link.
var node = mw.util.addPortletLink(
'p-personal',
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();
} );
// 時刻を表示。
showTime( $( node ).find( 'a:first' ) );
}
$( liveClock );
} );