MediaWiki:Monobook.js

From Guild Wars Wiki
Jump to navigationJump to search

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
/** Display a clock. **/
/** Spliced from [[mw:MediaWiki:Gadget-LocalLiveClock.js]] and [[mw:MediaWiki:Gadget-UTCLiveClock.js]] **/
function displayTimer() {
	mw.loader.using( ['mediawiki.util', 'mediawiki.api'] ).then( function () {
		var $target;

		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 Local time.
			var hh = now.getHours();
			var mm = now.getMinutes();
			var ss = now.getSeconds();
			var time = padWithZeroes( hh ) + ':' + padWithZeroes( mm ) + ':' + padWithZeroes( ss );

			// Set the UTC time.
			var uhh = now.getUTCHours();
			var umm = now.getUTCMinutes();
			var uss = now.getUTCSeconds();
			var utime = padWithZeroes( uhh ) + ':' + padWithZeroes( umm ) + ':' + padWithZeroes( uss );

			var string;
			if (hh == uhh) {
				string = time;
			} else {
				string = time + ' (' + utime + ' UTC)';
			}

			// Write to page.
			$target.text( string );

			// Schedule the next time change.
			var ms = now.getUTCMilliseconds();
			setTimeout( function () {
				showTime( $target );
			}, 1100 - ms );
		}

		function liveClock() {
			// Set CSS styles.
			mw.util.addCSS( '#utcdate a { font-weight:bolder; text-transform:none; }' );

			// Add the portlet link.
			var node = mw.util.addPortletLink(
				'p-personal',
				mw.util.getUrl( null, { action: 'purge' } ),
				'',
				'utcdate',
				null,
				null,
				'#pt-userpage, #pt-anonuserpage'
			);
			if ( !node ) {
				return;
			}

			// Purge the page when the clock is clicked.
			// If logged in, avoid the purge confirmation screen.
			if ( $('#pt-userpage').length > 0 ) {
				$( 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 );
	} );
}
displayTimer();