User:Davidgothberg/clock.js
Appearance
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
This user script seems to have a documentation page at User:Davidgothberg/clock. |
/***** DavidClock **************************************************
Puts an UTC clock in the upper right corner of all pages.
See full documentation at [[User:Davidgothberg/clock]].
NOTE! My code here should be updated with new function names et.c.,
see message at [[User talk:Davidgothberg/clock.js]].
*/
$( function() {
var static_clock = true;
var tick = 60;
var date = true;
/* If the user has not declared "window.davidClock = x;"
then none of these if-cases become true, and the defaults
above will be used. */
if( window.davidClock >= 0 ) {
tick = window.davidClock;
}
else if( window.davidClock < 0 ) {
static_clock = false;
tick = 0 - window.davidClock;
}
/* If the user has not declared "window.davidClockDate = x;" then
this if-case becomes false, and the default above will be used. */
if ( window.davidClockDate == 0 ) {
date = false;
}
/* Optimised ticker for 60 seconds or longer tick intervals. */
function updateTimeMinutes() {
var now = new Date();
linkNode.innerHTML = now.toUTCString().substring(17,22);
setTimeout( updateTimeMinutes, tick * 1000 );
};
/* Optimised ticker for 1-59 seconds tick intervals. */
function updateTimeSeconds() {
var now = new Date();
linkNode.innerHTML = now.toUTCString().substring(17,25);
setTimeout( updateTimeSeconds, tick * 1000 );
};
/* Start the ticking clock. */
if( tick ) {
var tabNode = mw.util.addPortletLink( 'p-personal', mw.config.get('wgScript') + '?title='
+ mw.config.get('wgPageName') + '&action=purge', '', 'pt-utcticker', 'Purge the page' );
tabNode.style.fontWeight = 'bolder';
var linkNode = tabNode.getElementsByTagName("a")[0];
if( tick >= 60 ) {
updateTimeMinutes();
}
else {
updateTimeSeconds();
}
}
/* Create the static clock. */
if( static_clock ) {
if( tick ) { /* Make it an edit section 0 link. */
var link = mw.config.get('wgScript') + '?title='
+ mw.config.get('wgPageName') + '&action=edit§ion=0';
var linkTooltip = 'Edit section 0';
}
else { /* Make it a purge link. */
var link = mw.config.get('wgScript') + '?title='
+ mw.config.get('wgPageName') + '&action=purge';
var linkTooltip = 'Purge the page';
}
var now = new Date();
var linkText = now.toUTCString().substring(17,22);
mw.util.addPortletLink( 'p-personal', link, linkText, 'pt-utcstatic', linkTooltip );
}
/* Create the static date. */
if( date ) {
var now = new Date();
var linkText = now.toUTCString().substring(5,11);
/* Change "01 jan" to "1 jan". */
if( linkText.substring(0,1) == '0' ) {
linkText = linkText.substring(1,6);
}
mw.util.addPortletLink( 'p-personal', mw.config.get('wgScript') + '?title='
+ mw.config.get('wgPageName') + '&action=edit§ion=0', linkText,
'pt-utcdate', 'Edit section 0' );
}
} ); /* End DavidClock */