User:Mr. Stradivarius/UTCLiveClock.js

/** * Warning! Global gadget file! * * This gadget adds a clock in the personal toolbar that shows the current time * in UTC, and also provides a link to purge the current page. * * Installation: * * 1. Add the following to the page MediaWiki:Gadget-UTCLiveClock.js on your * wiki: * *    mw.loader.load('//www.mediawiki.org/w/index.php?title=MediaWiki:Gadget-UTCLiveClock.js&action=raw&ctype=text/javascript'); * * 2. Copy the CSS page at https://www.mediawiki.org/wiki/MediaWiki:Gadget-UTCLiveClock.css * to the page MediaWiki:Gadget-UTCLiveClock.css on your wiki. * * 3. Copy the CSS page at https://www.mediawiki.org/wiki/MediaWiki:Gadget-UTCLiveClock-pagestyles.css * to the page MediaWiki:Gadget-UTCLiveClock-pagestyles.css on your wiki. * * 4. Add a description of the gadget to the page MediaWiki:Gadget-UTCLiveClock * on your wiki. You can use https://www.mediawiki.org/wiki/MediaWiki:Gadget-UTCLiveClock * as a template. * * 5. Add the following code to your wiki's MediaWiki:Gadgets-definition: * *     * UTCLiveClock[ResourceLoader|type=general|dependencies=mediawiki.util,mediawiki.api,mediawiki.notify|peers=UTCLiveClock-pagestyles]|UTCLiveClock.js|UTCLiveClock.css *    * UTCLiveClock-pagestyles[hidden|skins=vector,monobook]|UTCLiveClock-pagestyles.css * */ /*global mw, $ */ mw.loader.using( ['mediawiki.util', 'mediawiki.api', 'mediawiki.notify'] ).then( function { var $target;

function showTime( $target ) { var now = new Date; var hh = now.getUTCHours; var mm = now.getUTCMinutes; var ss = now.getUTCSeconds; var time = ( hh < 10 ? '0' + hh : hh ) + ':' + ( mm < 10 ? '0' + mm : mm ) + ':' + ( ss < 10 ? '0' + ss : ss ); $target.text( time );

var ms = now.getUTCMilliseconds;

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

function liveClock { mw.util.addCSS( '#utcdate a { font-weight:bolder; font-size:120%; }' ); $( '.client-js > body.skin-vector #p-personal ul' ).css( 'margin-right', 'initial' ); $( '.client-js > body.skin-monobook #p-personal ul' ).css( 'margin-right', 'initial' );

var node = mw.util.addPortletLink(		'p-personal',		mw.util.getUrl( null, { action: 'purge' } ),		'',		'utcdate'	); 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;	} );

showTime( $( node ).find( 'a:first' ) ); }

$( liveClock ); } );