User:Svick/transcludeWatchlist.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.
importScript('User:Luasóg bot/framework.js');
 
if (typeof jQuery == 'undefined')
  mw.loader.load('//bits.wikimedia.org/skins-1.5/common/jquery.min.js');

var timer = null;
var nextReload = null;
 
function loadWatchlist() {
  var watchlistElem = document.getElementById('transclude-watchlist');
  if (watchlistElem != null)
  {
    if (typeof transcludeWatchlistLimit == 'undefined')
      transcludeWatchlistLimit = 100;
 
    watchlistElem.innerHTML = '<div id="loading">Loading …</div>';
 
    var luasog = new Luasog("http://en.wikipedia.org/w/api.php");
 
    var requestParams = {action:"query", list: "watchlist", wlexcludeuser: mw.config.get('wgUserName'), wlprop: "ids|title|timestamp|user|parsedcomment|flags", wllimit: transcludeWatchlistLimit};
 
    var callback = function(data) {
      var list = document.createElement('ul');

      var target = (typeof transcludeWatchlistNewWindow != 'undefined' && transcludeWatchlistNewWindow) ? 'target="_blank"' : '';

      for (var i = 0; i < data.query.watchlist.length; i++) {
        var edit = data.query.watchlist[i];
        list.innerHTML += 
          '<li>' + 
            '(<a ' + target + ' href="/w/index.php?diff=prev&oldid=' + edit.revid + '">diff</a>) ' +
            '<b>' + (edit.minor == undefined ? '' : 'm') + (edit.bot == undefined ? '' : 'b') + '</b> ' +
            '<a ' + target + ' href="/wiki/' + escape(edit.title) + '">' + edit.title + '</a> ' +
            edit.timestamp + ' ' +
            '<a ' + target + ' href="/wiki/User:' + escape(edit.user) + '">' + edit.user + '</a> ' +
            '(' + edit.parsedcomment + ')' +
          '</li>';
      }

      var result = document.createElement('div');
      result.innerHTML += '<div id="time-to-reload" />';
      result.appendChild(list);

      var watchlistElem = document.getElementById('transclude-watchlist');
      watchlistElem.replaceChild(result, watchlistElem.firstChild);

      if (typeof transcludeWatchlistReloadTime != 'undefined' && timer == null) {
        refresh();
        timer = setInterval(refresh, 1000); // 1s
      }
    }
 
    luasog.request(requestParams, callback);
  }
}

function refresh() {
  if (nextReload == null) {
    nextReload = new Date();
    nextReload.setTime(nextReload.getTime() + transcludeWatchlistReloadTime * 1000);
  }

  var remaining = Math.floor((nextReload - new Date()) / 1000);

  if (remaining <= 0) {
    clearInterval(timer);
    timer = null;
    nextReload = null;
    loadWatchlist();
  } else {
    var timeToReloadElem = document.getElementById('time-to-reload');
    timeToReloadElem.innerHTML = 'Time to reload: ' + remaining + ' s.';
  }
}

$(loadWatchlist);