Jump to content

User:Davipo/wikipast.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.
// wikipast.js
// User:Davipo
// 2009-03-16, revised 2009-09-25

/* WikiPast: Browse Wikipedia as it appeared on a given date in the past.
             Works with TimeTraveller. 
*/

var pastdate = "1999-01-01";


function seeback() {
    var entry = prompt("Enter date:", pastdate);
    if (entry != null) {
        pastdate = entry;
        query_rev(wgArticleId, pastdate);
    }
}


function query_rev(pageid, date) {
    var qbase = "/w/api.php?action=query";
    var query = qbase + "&prop=revisions&rvlimit=1&rvprop=timestamp|ids&rvdir=older&format=json";
    query = query + "&pageids=" + pageid + "&rvstart=" + date + "T00:00:00Z";
    api.open('GET', wgServer + query, true);
    api.onreadystatechange = show_revision;
    api.send(null);
}


function show_revision() {
    if (api.readyState == 4) {
        if (api.status == 200) {
            var response = eval('(' + api.responseText + ')');
            var pageinfo = response['query']['pages'][wgArticleId];
            if (pageinfo['revisions']) {
                var revinfo = pageinfo['revisions'][0]; 
                var revid = revinfo['revid'];
                var timestamp = revinfo['timestamp'];
                var target_time = pastdate + 'T00:00:00Z';
                window.location = wgServer + '/w/index.php?oldid=' + revid + '&timetravel=' + target_time;
            }
        }
    }
}


function wpast_setup() {
    var tooltip = "Browse Wikipedia as it appeared on a past date";
    mw.util.addPortletLink("p-tb", "javascript:seeback()", "Browse in past", "t-past", tooltip, "`");
}

if(typeof sajax_init_object !== 'undefined') {
  var api = sajax_init_object();
  addOnloadHook(wpast_setup);
}