User:The Editor's Apprentice/vector.js

/** * This code is a modified version of the November 2017 revision of the code * found at https://www.mediawiki.org/wiki/MediaWiki:Gadget-UTCLiveClock.js * Boundless thanks to all of the people who have brought that code into * existance, it makes editing that much more amazing! * * This code adds a clock in the personal toolbar that shows the current time * and date in UTC in addition to providing a link to purge the current page. * * I would reccomend against using this code yourself since it is a bodge that * I'm using because I'm lazy. It does not integrate any of the CSS information * recommended for the gadget this is based upon. */ /*global mw, $ */ mw.loader.using( ['mediawiki.util', 'mediawiki.api'] ).then( function {

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;

// Get the month as a word. //function wordMonth { //var x = new Date; //var month = new Array(12); //month[0] = "January"; //month[1] = "February"; //month[2] = "March"; //month[3] = "April"; //month[4] = "May"; //month[5] = "June"; //month[6] = "July"; //month[7] = "August"; //month[8] = "September"; //month[9] = "October"; //month[10] = "November"; //month[11] = "December"; //var MM = month[x.getUTCMonth]; //}	//wordMonth;

// Get the day and time. // var dd = now.getUTCDay; var hh = now.getUTCHours; var mm = now.getUTCMinutes; var ss = now.getUTCSeconds; // Join together all time and date information. var time = padWithZeroes( hh ) + ':' + padWithZeroes( mm ) + ':' + padWithZeroes( ss ); $target.text( time );

// 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' );

// 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;	} );

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

$( liveClock ); } );