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 04:47, 19 September 2008 (Shorter ticker function code.). 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.

  It also has a link to purge the current page. (Click the clock 
  to purge the page.) If both the ticking and static clock is used,
  then the static clock has a link to edit section 0.

  To use this script put this code in your 
  [[User:Yourname/monobook.js]] page:

    importScript( "User:Davidgothberg/clock.js" );

  Default is a clock that ticks once a minute and a static clock 
  that shows when the page was loaded. This can be changed 
  by adding one of the lines below to your "/monobook.js" page.
  Don't set below 60 seconds on slow computers. 

    window.davidClock = -60;  //Only ticking clock, every minute.
    window.davidClock = -5;   //Only ticking clock, every 5 seconds.
    window.davidClock = 0;    //Only static clock
    window.davidClock = 5;    //Static + ticking clock, every 5 seconds.
    window.davidClock = 60;   //Static + ticking clock, every minute.
    window.davidClock = 120;  //Static + ticking clock, every 2 minutes.

  Technical details:
  Only thing that runs on each tick is the inner function 
  updateTimeMinutes() or updateTimeSeconds().

  Based on code from [[User:AzaToth]], [[User:Ilmari Karonen]] and 
  [[User:Voice of All]].
  Maintainer: [[User:Davidgothberg]]
*/


/* DavidClock */
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 */