MediaWiki:Gadget-utcclock.js

From RuneRealm Wiki

This is an old revision of this page, as edited by Alex (talk | contribs) at 00:10, 17 October 2024. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

Jump to navigation Jump to search

After saving, you may need to bypass your browser's cache to see the changes. For further information, see Wikipedia:Bypass your cache.

  • In most Windows and Linux browsers: Hold down Ctrl and press F5.
  • In Safari: Hold down ⇧ Shift and click the Reload button.
  • In Chrome and Firefox for Mac: Hold down both ⌘ Cmd+⇧ Shift and press R.
/**
 * Adapted from https://www.mediawiki.org/w/index.php?title=MediaWiki:Gadget-UTCLiveClock.js
 */

( 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();

	// Set the time.
	var hh = now.getUTCHours();
	var mm = now.getUTCMinutes();
	var time = padWithZeroes( hh ) + ':' + padWithZeroes( mm );
	$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() {
	// Add the portlet link.
	var node = mw.mw.util.addPortletLink(
		'p-personal', // portletId
		'#', // href
		'', // text
		'utcdate', // id
		'The current UTC time', // tooltip
		'', // accesskey
		'#pt-theme-toggles' // nextnode
	);
	if ( !node ) {
		return;
	}
	
	/*  We got a purge link in the More menu dropdown - this isn't needed
	// 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 );
} )();