User:Proteins/colorlinks.js

// // Change the color of article hyperlinks on the page // This is a warm-up exercise to coloring all article links by their assessment // // To use this script, add "importScript('User:Proteins/colorlinks.js');" to your monobook.js subpage // under your user page, as you can see at User:Proteins/monobook.js

function colorLinks { var alert_string = "";

var top_node;

var hyperlinks; var num_hyperlinks = 0; var hyperlink_index = 0;

var num_colored_links = 0; var num_redirect_links = 0;

var temp_link; var link_href; var link_title; var link_class;

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

// Loop over the hyperlinks for (hyperlink_index=0; hyperlink_index<num_hyperlinks; hyperlink_index++) { temp_link = hyperlinks[hyperlink_index]; if (!temp_link) { continue; }

link_href = temp_link.href; link_title = temp_link.title; link_class = temp_link.className;

if (link_class == "mw-redirect") { num_redirect_links++; temp_link.style.cssText = "background-color:" + "red"; continue; } 		if (link_class) { continue; } if ((!link_href) || (!link_title)) { continue; } if (link_title.match(/\(page does not exist\)$/ig)) { continue; } if (link_title.match(/^File:/ig)) { continue; } if (link_title.match(/^Image:/ig)) { continue; } if (link_title.match(/^Category:/ig)) { continue; } if (link_title.match(/^Special:/ig)) { continue; } if ((link_title.match(/^Edit\ssection:/i)) && (link_href.match(/action\=edit/i))) { continue; } if (link_href.match(/Talk:/ig)) { continue; } if (link_href.match(/Wikipedia:/ig)) { continue; } if (link_href.match(/Wikipedia talk:/ig)) { continue; } if (link_href.match(/Portal:/ig)) { continue; } if (link_href.match(/Portal talk:/ig)) { continue; } if (link_href.match(/User:/ig)) { continue; } if (link_href.match(/User talk:/ig)) { continue; } if (link_href.match(/Help:/ig)) { continue; } if (link_href.match(/Help talk:/ig)) { continue; } if (link_href.match(/Template:/ig)) { continue; } if (link_href.match(/Template talk:/ig)) { continue; } if (link_href.match(/Category:/ig)) { continue; } if (link_href.match(/Category talk:/ig)) { continue; } if (link_href.match(/File:/ig)) { continue; } if (link_href.match(/File talk:/ig)) { continue; } if (link_href.match(/MediaWiki:/ig)) { continue; } if (link_href.match(/MediaWiki talk:/ig)) { continue; }

num_colored_links++; temp_link.style.cssText = "color:" + "green";

} // closes loop over the hyperlinks

// Follow the randomly chosen link to the new location alert_string = "Colored " + num_colored_links + " article links green.\n"; alert_string += num_redirect_links + " redirected links are highlighted in red."; window.alert(alert_string);

} // closes function colorLinks addOnloadHook(function { mw.util.addPortletLink('p-navigation', 'javascript:colorLinks', 'Color links', 'ca-color', 'color article links', '@', ''); }); //