Jump to content

User:Svick/reverseWatchlist.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');

addOnloadHook(function() {
  if (mw.config.get('wgPageName') == 'Wikipedia:Reverse_Watchlist')
  {
    if (typeof reverseWatchlistLimit == 'undefined')
      reverseWatchlistLimit = 365;

    var startDate = new Date() - reverseWatchlistLimit * 24 * 60 * 60 * 1000;

    var hideMe = document.getElementById('hide-me');
    hideMe.parentNode.removeChild(hideMe);

    document.getElementById('bodyContent').innerHTML += '<div id="loading">Loading </div>';

    var luasog = new Luasog("http://en.wikipedia.org/w/api.php");

    var requestParams = {action:"query", generator: "watchlistraw", prop: "info|revisions", rvprop: "timestamp", gwrlimit: "max", gwrnamespace: "0"};
    var pages = new Array();

    var callback = function(data) {
      document.getElementById('loading').innerHTML += '.';

      for (var pageId in data.query.pages) {
        var page = data.query.pages[pageId];
        if (page.missing == undefined && page.redirect == undefined) {
          var date = new Date(page.revisions[0].timestamp);
          if (date < startDate)
            pages.push({title: page.title, timestamp: date});
        }
      }

      if (data['query-continue'] != undefined) {
        requestParams.gwrcontinue = data['query-continue'].watchlistraw.gwrcontinue;
        luasog.request(requestParams, callback);
      } else {
        pages.sort(function(a, b) { return a.timestamp - b.timestamp; });

        var hideMe = document.getElementById('loading');
        hideMe.parentNode.removeChild(hideMe);
        var result = document.createElement("ol");
        result.id = "reverse-watchlist";

        for (var i = 0; i < pages.length; i++) {
          var page = pages[i];
          var date = page.timestamp.toDateString();
          result.innerHTML += '<li>' + date.substring(date.indexOf(' ') + 1) + ' <a href="/wiki/' + escape(page.title) + '">' + page.title + '</a></li>';
        }
        document.getElementById('bodyContent').appendChild(result);
      }
    }

    luasog.request(requestParams, callback);
  }
});