User:Proteins/cullrandomlinksonpage.js

// // Generates a list of 20 random links in the article // // To use this script, add "importScript('User:Proteins/cullrandomlinksonpage.js');" to your monobook.js subpage // under your user page, as you can see at User:Proteins/monobook.js

function cullRandomLinksOnPage { 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;

var random_link_list = ""; var num_random_links = 0; var random_link_index = 0;

// 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; }

num_random_links = 15; random_link_index = 0; random_link_list = "";

while (random_link_index < num_random_links) {

// 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

// Add the randomly chosen link to the list

random_link_index++; if (bad_link == false) { random_link_list += random_link_title + "\n"; if (random_link_index%5 == 0) { random_link_list += "\n"; } } else { alert_string = "Unable to find a suitable random link " + random_link_index + " in " + max_num_iterations + " attempts.\n"; window.alert(alert_string); break; }	}

if (random_link_list) { alert_string = "Random assortment of " + random_link_index + " links from this page:\n\n"; alert_string += random_link_list; window.alert(alert_string); } } // closes function cullRandomLinksOnPage

$(function { mw.util.addPortletLink('p-navigation', 'javascript:cullRandomLinksOnPage', 'Cull random links', 'ca-cull', 'Cull 20 random links on this page', '2', ''); }); //