Jump to content

User:Davidgothberg/clock.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Davidgothberg (talk | contribs) at 12:40, 21 September 2008 (Moving the documentation to User:Davidgothberg/clock. (Not sure this is the best approach but testing it to get feedback.)). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
/***** DavidClock **************************************************

  Puts an UTC clock in the upper right corner of all pages.

  See full documentation at [[User:Davidgothberg/clock]].

*/
addOnloadHook( function() {

  var static = true;
  var tick = 60;

  /* 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 = false;
    tick = 0 - window.davidClock;
  }

  /* 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 = addPortletLink( 'p-personal', wgScriptPath 
      + '/index.php?title=' + encodeURIComponent(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 ) {

    if( tick ) {   /* Make it an edit section 0 link. */
      var link = wgScriptPath + '/index.php?title=' 
        + encodeURIComponent(wgPageName) + '&action=edit&section=0';
      var linkTooltip = 'Edit section 0';
    }
    else {   /* Make it a purge link. */
      var link = wgScriptPath + '/index.php?title=' 
        + encodeURIComponent(wgPageName) + '&action=purge';
      var linkTooltip = 'Purge the page';
    }

    var now = new Date();
    var linkText = now.toUTCString().substring(17,22);
 
    addPortletLink( 'p-personal', link, linkText, 'pt-utcstatic', linkTooltip );
  }

} );  /* End DavidClock */