User:Proteins/articlerewind.js

// // View the article on a previous date // // To use this script, add "importScript('User:Proteins/articlerewind.js');" to your monobook.js subpage // under your user page, as you can see at User:Proteins/monobook.js

function articleRewind { var alert_string = ""; var prompt_string = "";

var timestamp_string;

var temp_unordered_list; var list_items; var temp_list_item;

var hyperlinks; var temp_hyperlink; var num_hyperlinks = 0;

var random_double = 0.5; var random_hyperlink_index = 0;

var random_link; var random_link_href = ""; var random_link_title = "";

var num_iterations = 0; var max_num_iterations = 100;

var bad_link = true;

var page_name; var article_name;

var new_window; var new_document; var new_window_href;

// Get the time stamp prompt_string = "Please enter the date/time you'd like to rewind to.\n\n"; prompt_string += "The format is YYYYMMDDhhmmss or abbreviations thereof\n"; prompt_string += "(Y=year, M=month, D=day, h=hour, m=minute, s=second)\n\n"; prompt_string += "A simple example is 20070811 for 11 August 2007\n"; prompt_string += "Another example is 2008042184527 for 22 April 2008 at 18:45:27 UTC.\n"; timestamp_string = window.prompt(prompt_string, "20080422");

// Get the article's own name page_name = wgPageName; article_name = (wgNamespaceNumber == 1) ? page_name.substr(5) : page_name; // Handle encoded characters, i.e. \x26 in wgPageName = %26 in URL article_name = URLEncode(article_name); //	window.alert("Article name: " + article_name);

// Open new window with the article history new_window_href = "http://en.wikipedia.org/w/index.php?title=" + article_name + "&action=history&limit=1&offset=" + timestamp_string; new_window = window.open(new_window_href, "new_window"); new_window.focus; new_window.alert("Opened history window."); //	new_window.location.reload(true);

// Get the new document new_document = new_window.document; if (!new_document) { new_window.alert("There is no document in the new window."); return; }	new_window.alert("Got the new document in the new window.");

// Get the pagehistory temp_unordered_list = new_document.getElementById('pagehistory'); if (!temp_unordered_list) { new_window.location.reload(true); new_document = new_window.document; temp_unordered_list = new_document.getElementById('pagehistory'); }	if (!temp_unordered_list) { new_window.alert("There is no pagehistory node in this article."); return; }	new_window.alert("Got the page history");

// Get first list item list_items = temp_unordered_list.getElementsByTagName('LI'); temp_list_item = list_items[0]; if (!temp_list_item) { new_window.alert("There is no pagehistory list item in this article."); return; } //	new_window.alert("Got the first list item.");

// Get the hyperlinks hyperlinks = temp_list_item.getElementsByTagName("A"); if (!hyperlinks) { new_window.alert("There are no hyperlinks in this list item."); return; }	num_hyperlinks = hyperlinks.length; if (num_hyperlinks < 3) { new_window.alert("There are less than three hyperlinks in this list item."); return; } // get the third anchor temp_hyperlink = hyperlinks[2]; if (!temp_hyperlink) { new_window.alert("Unable to get third hyperlink of this list item."); return; } //	new_window.alert("Got the third hyperlink"); new_window.location = temp_hyperlink.href;

} // closes function articlerewind

$(function { mw.util.addPortletLink('p-cactions', 'javascript:articleRewind', 'rewind', 'ca-rewind', 'Rewind article to a previous date', '<', ''); }); //