User:Alex 21/script-categorypagelinks.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.
$(document.getElementsByClassName("mw-category-generated")).ready(function() {
	// Categories only
	if (mw.config.get("wgNamespaceNumber") !== 14) return;
	
	// Make sure it's only run once
	var mw_category_alllinks = document.getElementsByClassName("categorypagelinks");
	if (mw_category_alllinks.length !== 0) return;
	
	// Variables
	var baseLink = "https://en.wikipedia.org/wiki/";
	var i, anchor_span, anchor_edit, anchor_talk, anchor_hist, mw_category_anchor, mw_category_article, documentFragment;
	
	// All anchors within the main category class
	var mw_category = document.getElementsByClassName("mw-category-generated");
	var mw_category_allanchors = mw_category[0].getElementsByTagName("a");
	
	// Add specific class as a failsafe so the script only runs once, and only on these anchors and not the ones generated later
	for (i = 0; i < mw_category_allanchors.length; i++) {
		mw_category_anchor = mw_category_allanchors[i];
		mw_category_anchor.className = mw_category_anchor.className+" categorypagelinks";
	}
	
	mw_category_alllinks = document.getElementsByClassName("categorypagelinks");

	for (i = 0; i < mw_category_alllinks.length; i++) {
		// Get anchor and href attribute
		mw_category_anchor = mw_category_alllinks[i];
		mw_category_article = mw_category_anchor.href.substr(baseLink.length);
		
		if (mw_category_article.indexOf("Wikipedia:FAQ") != -1) continue;
		
		// Create edit, talk and history links
		anchor_edit = document.createElement("a");
		anchor_talk = document.createElement("a");
		anchor_hist = document.createElement("a");
		anchor_edit.innerHTML = "edit";
		anchor_talk.innerHTML = "talk";
		anchor_hist.innerHTML = "history";
		anchor_edit.className = "external text";
		anchor_hist.className = "external text";
		
		anchor_edit.href = "https://en.wikipedia.org/w/index.php?title="+mw_category_article+"&action=edit";
		anchor_talk.href = "https://en.wikipedia.org/wiki/Talk:"+mw_category_article;
		anchor_hist.href = "https://en.wikipedia.org/w/index.php?title="+mw_category_article+"&action=history";
		
		// Span to house the edit, talk and history links 
		anchor_span = document.createElement("span");
		anchor_span.className = "plainlinks lx";
		
		// Lay out links as such: ( edit | talk | history )
		anchor_span.appendChild(document.createTextNode(" ("));
		anchor_span.appendChild(anchor_edit);
		anchor_span.appendChild(document.createTextNode(" | "));
		anchor_span.appendChild(anchor_talk);
		anchor_span.appendChild(document.createTextNode(" | "));
		anchor_span.appendChild(anchor_hist);
		anchor_span.appendChild(document.createTextNode(")"));
		
		// Add span and links
		documentFragment = document.createDocumentFragment();
		documentFragment.appendChild(anchor_span);
		mw_category_anchor.parentNode.appendChild(documentFragment);
	}
});