User:Alex 21/script-categorypagelinks.js
Appearance
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
This user script seems to have a documentation page at User:Alex 21/script-categorypagelinks. |
$(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);
}
});