User:HueSatLum/pendingChangesLink.js

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.
( function( $, mw ) {
  var baseTooltip = 'List of pages with unreviewed edits'
  mw.util.addPortletLink(
    'p-tb', // toolbox ID
    mw.util.getUrl( 'Special:PendingChanges' ), // Pending changes link
    'Pending changes', // link text
    't-pendingchanges', // link ID
    baseTooltip // link tooltip
  );
  
  function plural( num, text ) {
    return num + ' ' + text + ( num !== 1 ? 's' : '' );
  }
  
  function updateNumChanges() {
    new mw.Api().get( {
      action: 'query',
      list: 'oldreviewedpages',
      orlimit: 40,
      ornamespace: [ 0, 4 ]
    } ).done( function ( data ) {
      if ( !data.query ) {
      	return;
      }
      
      var numChanges = data.query.oldreviewedpages.length,
          oldestTimestamp = null;
      
      if ( numChanges > 0 ) {
        oldestTimestamp = data.query.oldreviewedpages[0].pending_since;
        var then = new Date( oldestTimestamp ).getTime(),
            now = Date.now(),
            msDiff = now - then,
            hourDiff = Math.round( msDiff / 36e5 );
      }
      
      if( data.continue ){
        numChanges += '+';
      }
      
      $( '#t-pendingchanges a' )
        .text( plural( numChanges, 'pending edit' ) )
        .attr( 'title', function () {
          if ( !oldestTimestamp ) {
            return baseTooltip;
          }
          return baseTooltip + '.\nOldest is ' + plural( hourDiff, 'hour' ) + ' old.';
        } );
    });
  }
  
  updateNumChanges();
  setInterval( updateNumChanges, 2 * 60000 ); // update every 2 minutes
} ) ( jQuery, mediaWiki );