User:Proteins/followrandomlinkonpage.js

// // Travel to a random page linked in the article (a so-called "aleation" or an "aleatory fugue") // // To use this script, add "importScript('User:Proteins/followrandomlinkonpage.js');" to your monobook.js subpage // under your user page, as you can see at User:Proteins/monobook.js

function followRandomLinkOnPage { var alert_string = "";

var top_node;

var hyperlinks; 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;

// Get the bodyContent node top_node = document.getElementById('bodyContent'); if (!top_node) { window.alert("There is no bodyContent node in this article."); return; } // Get a list of the hyperlinks hyperlinks = top_node.getElementsByTagName("A"); if (!hyperlinks) { window.alert("There are no hyperlinks in this article."); return; }	num_hyperlinks = hyperlinks.length; if (num_hyperlinks < 1) { window.alert("There are no hyperlinks in this article."); return; } // Choose a random hyperlink

bad_link = true; num_iterations = 0; random_link = null; random_link_href = ""; random_link_title = "";

while ((num_iterations= num_hyperlinks)) { continue; }

random_link = hyperlinks[random_hyperlink_index]; random_link_href = random_link.href; random_link_title = random_link.title;

if ((!random_link) || (!random_link_title) || (!random_link_title)) { // Check that all three exist continue; } else if (random_link.className) { // Don't follow external links continue; } else if (random_link_title.match(/\(page does not exist\)$/ig)) { // Don't follow red links continue; } else if (random_link_title.match(/^Image:/ig)) { // Don't follow Image links continue; } else if (random_link_title.match(/^Category:/ig)) { // Don't follow Category links continue; } else if (random_link_title.match(/^Special:/ig)) { // Don't follow links to Special pages continue; } else if ((random_link_title.match(/^Edit\ssection:/i)) && (random_link_href.match(/action\=edit/i))) { // Don't follow edit links continue; }

if (wgNamespaceNumber == 0) { // special behavior for Main article space if (random_link.id) { continue; } if (random_link_href.match(/Talk:/ig)) { continue; } if (random_link_href.match(/Wikipedia:/ig)) { continue; } if (random_link_href.match(/Wikipedia talk:/ig)) { continue; } if (random_link_href.match(/Portal:/ig)) { continue; } if (random_link_href.match(/Portal talk:/ig)) { continue; } if (random_link_href.match(/User:/ig)) { continue; } if (random_link_href.match(/User talk:/ig)) { continue; } if (random_link_href.match(/Help:/ig)) { continue; } if (random_link_href.match(/Help talk:/ig)) { continue; } if (random_link_href.match(/Template:/ig)) { continue; } if (random_link_href.match(/Template talk:/ig)) { continue; } if (random_link_href.match(/Category:/ig)) { continue; } if (random_link_href.match(/Category talk:/ig)) { continue; } if (random_link_href.match(/File:/ig)) { continue; } if (random_link_href.match(/File talk:/ig)) { continue; } if (random_link_href.match(/MediaWiki:/ig)) { continue; } if (random_link_href.match(/MediaWiki talk:/ig)) { continue; } } // closes check for Main article space

bad_link = false;

} // closes loop generating a random hyperlink

// Follow the randomly chosen link to the new location

if (bad_link == false) { alert_string = "Follow link to the page \"" + random_link_title + "\"\?\n"; if (window.confirm(alert_string) == true) { window.open(random_link_href); }	} else { alert_string = "Unable to find a suitable random link in " + max_num_iterations + " attempts.\n"; window.alert(alert_string); }

} // closes function followRandomLinkOnPage

addOnloadHook(function { mw.util.addPortletLink('p-navigation', 'javascript:followRandomLinkOnPage', 'Follow a lucky link!', 'ca-lucky', 'Follow a randomly chosen link on this page', '@', ''); }); //