User:Davipo/wikipast.js
Appearance
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
Documentation for this user script can be added at User:Davipo/wikipast. |
// 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);
}