Jump to content

User:Davidgothberg/clock.js

From Wikipedia, the free encyclopedia
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]].

   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&section=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&section=0', linkText,
      'pt-utcdate', 'Edit section 0' );
  }

} );  /* End DavidClock */