MediaWiki:Gadget-utcclock.js: Difference between revisions
Jump to navigation
Jump to search
Content added Content deleted
No edit summary Tag: Manual revert |
No edit summary |
||
Line 1: | Line 1: | ||
"use strict"; |
|||
/** |
/** |
||
* Adapted from https://www.mediawiki.org/w/index.php?title=MediaWiki:Gadget-UTCLiveClock.js |
* 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 |
|||
function padWithZeroes( num ) { |
|||
// 0 <= num < 100. |
|||
// Pad a number with zeroes. The number must be an integer where |
|||
return num < 10 ? '0' + num.toString() : num.toString(); |
|||
// 0 <= num < 100. |
|||
} |
|||
return num < 10 ? '0' + num.toString() : num.toString(); |
|||
function showTime($target) { |
|||
} |
|||
var now = new Date(); |
|||
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 ); |
|||
// Set the time. |
|||
var hh = now.getUTCHours(); |
|||
// |
|||
var mm = now.getUTCMinutes(); |
|||
// We schedule the change for 100 ms _after_ the next clock tick. The delay |
|||
var time = padWithZeroes(hh) + ':' + padWithZeroes(mm); |
|||
// from setTimeout is not precise, and if we aim exactly for the tick, there |
|||
$target.text(time); |
|||
// 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 ); |
|||
} |
|||
// Schedule the next time change. |
|||
function liveClock() { |
|||
// |
|||
// Add the portlet link. |
|||
// We schedule the change for 100 ms _after_ the next clock tick. The delay |
|||
var node = mw.util.addPortletLink( |
|||
// from setTimeout is not precise, and if we aim exactly for the tick, there |
|||
'p-personal', // portletId |
|||
// is a chance that the function will run slightly before it. If this |
|||
'#', // href |
|||
// happens, we will display the same time for two seconds in a row - not |
|||
'', // text |
|||
// good. By scheduling 100 ms after the tick, we will always be about 100 ms |
|||
'utcdate', // id |
|||
// late, but we are also very likely to display a new time every second. |
|||
'The current UTC time', // tooltip |
|||
var ms = now.getUTCMilliseconds(); |
|||
'', // accesskey |
|||
setTimeout(function () { |
|||
'#pt-theme-toggles' // nextnode |
|||
showTime($target); |
|||
); |
|||
}, 1100 - ms); |
|||
if ( !node ) { |
|||
} |
|||
return; |
|||
function liveClock() { |
|||
} |
|||
// Add the portlet link. |
|||
var node = mw.util.addPortletLink('p-personal', |
|||
/* We got a purge link in the More menu dropdown - this isn't needed |
|||
// portletId |
|||
// 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. |
|||
// href |
|||
$( node ).on( 'click', function ( e ) { |
|||
'', |
|||
new mw.Api().post( { action: 'purge', titles: mw.config.get( 'wgPageName' ) } ).then( function () { |
|||
// text |
|||
location.reload(); |
|||
'utcdate', |
|||
}, function () { |
|||
// id |
|||
mw.notify( 'Purge failed', { type: 'error' } ); |
|||
'The current UTC time', |
|||
} ); |
|||
// tooltip |
|||
e.preventDefault(); |
|||
'', |
|||
} ); |
|||
// accesskey |
|||
*/ |
|||
'#pt-theme-toggles' // nextnode |
|||
); |
|||
if (!node) { |
|||
return; |
|||
} |
|||
/* We got a purge link in the More menu dropdown - this isn't needed |
|||
// Show the clock. |
|||
// Purge the page when the clock is clicked. We have to do this through the |
|||
showTime( $( node ).find( 'a:first' ) ); |
|||
// 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. |
|||
$( liveClock ); |
|||
showTime($(node).find('a:first')); |
|||
} )(); |
|||
} |
|||
$(liveClock); |
|||
})(); |
Latest revision as of 12:06, 20 October 2024
"use strict";
/**
* 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);
})();