Jump to content

User:Proteins/listredlinks.js

From Wikipedia, the free encyclopedia
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>
// Produce an ordered list of redlinks on a page, with links for searching out more information
//
// To use this script, add "importScript('User:Proteins/listredlinks.js');" to your monobook.js subpage 
// under your user page, as you can see at User:Proteins/monobook.js

function listRedLinks() { 
	var alert_string = "";
 
	var child_node;
 
	var user_name = "";
	var user_page_link;
 
	var body_content;
	var main_article_paragraphs;
	var first_paragraph_in_main_article;

	var hyperlinks;
	var num_hyperlinks = 0;

	var temp_hyperlink;
	var temp_hyperlink_title = "";

	var num_redlinks = 0;
	var redlink_index = 0;
	var redlink_list = new Array();

	var redlink_title = "";
	var redlink_title_list = new Array();

	var redlink_list_div;
	var redlink_list_item;
	var redlink_ordered_list;

	var lead_section_heading;
	var redlink_section_heading;

// Get the bodyContent node
	body_content = document.getElementById('bodyContent');
	if (!body_content) { 
		alert_string = "ERROR: There is no bodyContent node in this article.";
		window.alert(alert_string);
		return;
	}
	main_article_paragraphs = body_content.getElementsByTagName("P");
	if (!main_article_paragraphs) { 
		alert_string = "ERROR: The bodyContent node has no paragraphs.";
		window.alert(alert_string);
		return;
	}
	first_paragraph_in_main_article = main_article_paragraphs[0];
	if (!first_paragraph_in_main_article) { 
		alert_string = "ERROR: The main article has no first paragraph.";
		window.alert(alert_string);
		return;
	}
 

// Get a list of the hyperlinks 
	hyperlinks = body_content.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;
	}


// Create a list of redlinks by looping over the hyperlinks
	num_redlinks = 0;
	redlink_title = "";
	for (hyperlink_index=0; hyperlink_index<num_hyperlinks; hyperlink_index++) { 
		temp_hyperlink = hyperlinks[hyperlink_index];
		if (!temp_hyperlink) { continue; } 

		temp_hyperlink_title = temp_hyperlink.title;
		if (temp_hyperlink_title.match(/\(page does not exist\)$/ig)) {
			num_redlinks++;
			redlink_list.push(temp_hyperlink);

			redlink_title = temp_hyperlink_title.replace(/\(page does not exist\)$/ig,"");
			redlink_title = redlink_title.replace(/\s\([^\)]\)$/ig,"");
			redlink_title = redlink_title.replace(/\s+$/ig,"");
			redlink_title_list.push(redlink_title);
		}

	} // closes loop over hyperlinks in the bodyContent

	if (num_redlinks == 0) { 
		window.alert("This article has no redlinks - congratulations!");
		return;
	}


// Print list of redlinks
	alert_string = "";
	for (redlink_index=0; redlink_index<num_redlinks; redlink_index++) { 
		alert_string += "Redlink " + redlink_index + ". " + redlink_title_list[redlink_index] + "\n";

	} // closes loop over the red links
//	window.alert(alert_string);


// Create a DIV element to enclose the redlink list and insert it into the present article
	redlink_list_div = document.createElement("DIV");
	if (!redlink_list_div) { 
		alert_string = "ERROR: Unable to create a new DIV element to hold the list of redlinks.";
		window.alert(alert_string);
		return;
	}
	redlink_list_div.className = "redlink-list-div";
	body_content.insertBefore(redlink_list_div, first_paragraph_in_main_article);
 
	alert_string = "Inserted a new DIV element ahead of the first paragraph in the main article.";
//	window.alert(alert_string);

// Add lead section heading
	lead_section_heading = document.createElement("HR");
	if (!lead_section_heading) { 
		alert_string = "ERROR: Unable to create a new horizontal rule ahead of the lead section";
		window.alert(alert_string);
		return;
	}
//	lead_section_heading.appendChild(document.createTextNode("Lead section"));
	body_content.insertBefore(lead_section_heading, first_paragraph_in_main_article);


// Add redlink section heading
	redlink_section_heading = document.createElement("H2");
	if (!redlink_section_heading) { 
		alert_string = "ERROR: Unable to create a new section heading for the redlink section";
		window.alert(alert_string);
		return;
	}
	redlink_section_heading.appendChild(document.createTextNode("Article redlinks"));
	redlink_list_div.appendChild(redlink_section_heading);

	if (num_redlinks == 1) { 
		redlink_list_div.appendChild(document.createTextNode("This article has one redlink.  This is listed below, with links to search for information."));
	} else { 
		redlink_list_div.appendChild(document.createTextNode("This article has " + num_redlinks + " redlinks.  These are listed below, with links to search for information."));
	}

// Create ordered list of redlinks
	redlink_ordered_list = document.createElement("OL");
	if (!redlink_ordered_list) { 
		alert_string = "ERROR: Unable to create a new OL element to hold the list of redlinks.";
		window.alert(alert_string);
		return;
	}
	redlink_ordered_list.className = "redlink-list";
	redlink_list_div.appendChild(redlink_ordered_list);
 
	alert_string = "Inserted a new OL element into the redlink DIV element.";
//	window.alert(alert_string);


// Add the redlinks to the ordered list
	for (redlink_index=0; redlink_index<num_redlinks; redlink_index++) { 
		redlink_list_item = document.createElement("LI");
		if (!redlink_ordered_list) { 
			alert_string = "ERROR: Unable to create a new LI element for redlink " + redlink_index + "\n";
			window.alert(alert_string);
			continue;
		}
		redlink_ordered_list.appendChild(redlink_list_item);

		temp_hyperlink = redlink_list[redlink_index];
		if (!temp_hyperlink) { 
			alert_string = "ERROR: Unable to retrieve redlink " + redlink_index + "\n";
			window.alert(alert_string);
			continue;
		}
		redlink_list_item.appendChild(temp_hyperlink.cloneNode(true));
		redlink_list_item.appendChild(document.createElement("BR"));
		redlink_list_item.appendChild(document.createTextNode("    Search: "));

		temp_hyperlink = document.createElement("A");
		temp_hyperlink.target = "_blank";
		temp_hyperlink.href = "http://en.wikipedia.org/wiki/Special:Search?search=" + redlink_title_list[redlink_index].replace(/\s/g, "+");
		temp_hyperlink.appendChild(document.createTextNode("Wikipedia"));
		redlink_list_item.appendChild(temp_hyperlink);
		redlink_list_item.appendChild(document.createTextNode(" / "));

		temp_hyperlink = document.createElement("A");
		temp_hyperlink.target = "_blank";
		temp_hyperlink.href = "http://www.google.com/search?q=\"" + redlink_title_list[redlink_index].replace(/\s/g, "+") + "\"";
		temp_hyperlink.appendChild(document.createTextNode("Google"));
		redlink_list_item.appendChild(temp_hyperlink);
		redlink_list_item.appendChild(document.createTextNode(" / "));

		temp_hyperlink = document.createElement("A");
		temp_hyperlink.target = "_blank";
		temp_hyperlink.href = "http://scholar.google.com/scholar?q=\"" + redlink_title_list[redlink_index].replace(/\s/g, "+") + "\"";
		temp_hyperlink.appendChild(document.createTextNode("Google Scholar"));
		redlink_list_item.appendChild(temp_hyperlink);
		redlink_list_item.appendChild(document.createTextNode(" / "));

		temp_hyperlink = document.createElement("A");
		temp_hyperlink.target = "_blank";
		temp_hyperlink.href = "http://books.google.com/books?&as_brr=0&q=\"" + redlink_title_list[redlink_index].replace(/\s/g, "+") + "\"";
		temp_hyperlink.appendChild(document.createTextNode("Google Books"));
		redlink_list_item.appendChild(temp_hyperlink);
		redlink_list_item.appendChild(document.createTextNode(" / "));

		temp_hyperlink = document.createElement("A");
		temp_hyperlink.target = "_blank";
		temp_hyperlink.href = "http://news.google.com/archivesearch?&as_src=-newswire+-wire+-presswire+-PR+-press+-release+-wikipedia&q=\"" + redlink_title_list[redlink_index].replace(/\s/g, "+") + "\"";
		temp_hyperlink.appendChild(document.createTextNode("Google News"));
		redlink_list_item.appendChild(temp_hyperlink);
		redlink_list_item.appendChild(document.createTextNode(" / "));

		temp_hyperlink = document.createElement("A");
		temp_hyperlink.target = "_blank";
		temp_hyperlink.href = "http://www.ncbi.nlm.nih.gov/entrez/query.fcgi?db=pubmed&term=" + redlink_title_list[redlink_index].replace(/\s/g, "+");
		temp_hyperlink.appendChild(document.createTextNode("PubMed"));
		redlink_list_item.appendChild(temp_hyperlink);
		redlink_list_item.appendChild(document.createTextNode(" / "));

		temp_hyperlink = document.createElement("A");
		temp_hyperlink.target = "_blank";
		temp_hyperlink.href = "http://www.ncbi.nlm.nih.gov/entrez/query.fcgi?db=pmc&term=" + redlink_title_list[redlink_index].replace(/\s/g, "+");
		temp_hyperlink.appendChild(document.createTextNode("PubMedCentral"));
		redlink_list_item.appendChild(temp_hyperlink);
		redlink_list_item.appendChild(document.createTextNode(" / "));

		temp_hyperlink = document.createElement("A");
		temp_hyperlink.target = "_blank";
		temp_hyperlink.href = "http://scienceworld.wolfram.com/search/index.cgi?as_q=" + redlink_title_list[redlink_index].replace(/\s/g, "+");
		temp_hyperlink.appendChild(document.createTextNode("WorldofScience"));
		redlink_list_item.appendChild(temp_hyperlink);
		redlink_list_item.appendChild(document.createTextNode(" / "));

		temp_hyperlink = document.createElement("A");
		temp_hyperlink.target = "_blank";
		temp_hyperlink.href = "http://www.britannica.com/bps/search?query=" + redlink_title_list[redlink_index].replace(/\s/g, "+");
		temp_hyperlink.appendChild(document.createTextNode("Britannica"));
		redlink_list_item.appendChild(temp_hyperlink);
		redlink_list_item.appendChild(document.createTextNode(" / "));

		temp_hyperlink = document.createElement("A");
		temp_hyperlink.target = "_blank";
		temp_hyperlink.href = "http://encarta.msn.com/encnet/refpages/search.aspx?q=" + redlink_title_list[redlink_index].replace(/\s/g, "+");
		temp_hyperlink.appendChild(document.createTextNode("Encarta"));
		redlink_list_item.appendChild(temp_hyperlink);

	} // closes loop over the red links
//	redlink_list_div.appendChild(document.createTextNode("Please suggest other databases to search through to User:Proteins; thank you!"));
	redlink_list_div.appendChild(document.createElement("BR"));


// Print acknowledgment 
	alert_string = "Created ordered list of " + num_redlinks + " redlinks.\n";
//	window.alert(alert_string);

} // closes function listRedLinks()

$(function () {
mw.util.addPortletLink('p-navigation', 'javascript:listRedLinks()', 'Redlinks & search', 'ca-redlinks', 'Ordered list of redlinks', '!', '');
});
 
//</pre>