User:Proteins/showrandomlinksonpage.js

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.
//<pre>
// Create a slideshow of random articles linked in the article (a so-called "aleation" or an "aleatory fugue")
//
// To use this script, add "importScript('User:Proteins/showrandomlinksonpage.js');" to your monobook.js subpage 
// under your user page, as you can see at User:Proteins/monobook.js

// Global variables
var new_window;
var wait_time = 0;
var random_link_index = 0;
var random_link_href_list = new Array();
var random_link_title_list = new Array();

function timeoutRoutine()
{
	alert("Wait = " + wait_time + "  Slide " + random_link_index + ": " + random_link_title_list[random_link_index]);
	new_window.location = random_link_href_list[random_link_index];
}

function showRandomLinksOnPage() { 
	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 num_random_links = 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 = 5;
	random_link_index = 0;
	random_link_href_list[0] = "";
	random_link_title_list[0] = "";

	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<max_num_iterations) && (bad_link)) {
			num_iterations++;

			random_double = num_hyperlinks * Math.random();
			random_hyperlink_index = Math.floor(random_double);
			if ((random_hyperlink_index < 0) || (random_hyperlink_index >= 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

		if (bad_link == false) {
			random_link_index++;
			random_link_href_list[random_link_index] = random_link_href;
			random_link_title_list[random_link_index] = random_link_title;
		} else {
			alert_string = "Unable to find a suitable random link " + random_link_index + " in " + max_num_iterations + " attempts.\n";
			window.alert(alert_string);
			break;
		}
	}
	
	num_random_links = random_link_index;
	if (num_random_links > 0) { 
		alert_string = "Show slideshow of " + num_random_links + " random links from this page\?\n\n";
		for (random_link_index=1; 
		     random_link_index<=num_random_links; 
		     random_link_index++) {
			alert_string += random_link_title_list[random_link_index] + "\n";
			if (random_link_index%5 == 0) { alert_string += "\n"; }
		}
		if (window.confirm(alert_string) == false) { return; }
			
		random_link_href = random_link_href_list[1];
		new_window = window.open(random_link_href, "new_window");
		random_link_index = 2;
		new_window.setTimeout("timeoutRoutine();", 3000);
//		for (random_link_index=2; 
//		     random_link_index<=num_random_links; 
//		     random_link_index++) {
//				if (window.confirm("Continue slideshow to \"" + random_link_title_list[random_link_index] + "\"\?") == false) { return; }
//				random_link_href = random_link_href_list[random_link_index];
//				new_window.location = random_link_href;
//				wait_time = 3000 * (random_link_index - 1);
//				new_window.setTimeout("timeoutRoutine();", wait_time);
//new_window.setTimeout("new_window.location=" + random_link_href_list[random_link_index]+ ";",wait_time);
//   new_window.setTimeout("if (new_window.confirm('Continue slideshow?') == false) { break; }",800);
//		}
	}
} // closes function showRandomLinksOnPage()

$(function () {
mw.util.addPortletLink('p-navigation', 'javascript:showRandomLinksOnPage()', 'Show random links', 'ca-slideshow', 'Show sequence of random links from this page', '$', '');
});
 
//</pre>