MediaWiki:Gadget-utcclock.js: Difference between revisions

From RuneRealm Wiki
Jump to navigation Jump to search
Content added Content deleted
No edit summary
Tag: Reverted
No edit summary
Tag: Manual revert
Line 36: Line 36:
function liveClock() {
function liveClock() {
// Add the portlet link.
// Add the portlet link.
var node = mw.mw.util.addPortletLink(
var node = mw.util.addPortletLink(
'p-personal', // portletId
'p-personal', // portletId
'#', // href
'#', // href

Revision as of 17:15, 17 October 2024

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