MediaWiki:Gadget-catfix.js
Appearance
Note: You may have to bypass your browser’s cache to see the changes. In addition, after saving a sitewide CSS file such as MediaWiki:Common.css, it will take 5-10 minutes before the changes take effect, even if you clear your cache.
- Mozilla / Firefox / Safari: hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (Command-R on a Macintosh);
- Konqueror and Chrome: click Reload or press F5;
- Opera: clear the cache in Tools → Preferences;
- Internet Explorer: hold Ctrl while clicking Refresh, or press Ctrl-F5.
- The following documentation is located at MediaWiki:Gadget-catfix.js/documentation. [edit]
- This script is a part of the
catfix
gadget (edit definitions)- Description (edit): Processes links to entries in language-specific categories such as Category:English lemmas, adding language-specific formatting and pointing them to the language's section of the entry on by default, disable at your own risk
- Useful links: subpage list • links • redirects • transclusions
// <nowiki>
// Makes tweaks to links in language-specific categories, where we would normally
// be wrapping them in {{l}}.
for (let catfix of document.getElementsByClassName("catfix")) {
// Catfix looks something like this:
// <ul class="catfix" data-anchor="Russian"><span class="Cyrl" lang="ru"></span></ul>
// The outer element records the anchor that we need to use, while the child element is used as the wrapper.
// Applies a language-specific wrapper to the link based on the information from `catfix`.
// If the link is something like `Appendix:Toki Pona/nasin`,
// we need to wrap it like this: `Appendix:Toki Pona/<span prop="blah blah">nasin</span>`.
// Otherwise, put the span around the link and add an anchor.
function processLink(link) {
if (link.catfixed) return;
let wrapper = catfix.firstElementChild.cloneNode();
let pageName = link.textContent;
let ns = (new mw.Title(pageName)).getNamespaceId();
if (ns === 0) { // Mainspace
link.parentElement.append(wrapper);
wrapper.append(link);
link.hash = catfix.dataset.anchor;
} else if (ns === 100 && pageName.includes("/")) { // Appendix
let [prefix, ...title] = pageName.split("/");
// only do Appendix:ABC/def if ABC matches the catfix language
if (prefix.replace('Appendix:', '') === catfix.dataset.anchor.replaceAll('_', ' ')) {
title = title.join('/');
wrapper.textContent = title;
link.innerHTML = "";
link.append(prefix + "/", wrapper);
}
} else if (ns === 118) { // Reconstruction
// experiment: try replacing Reconstruction:XYZ/ with * for a cleaner layout
// (but only if XYZ matches the catfix's language!)
let [prefix, ...title] = pageName.split("/");
if (prefix.replace('Reconstruction:', '') === catfix.dataset.anchor.replaceAll('_', ' ')) {
title = title.join('/');
link.textContent = "*" + title;
link.parentElement.append(wrapper);
wrapper.append(link);
}
}
link.catfixed = true;
}
// There are currently two types of catfixes: those which apply to an entire category page,
// And those applying to a single category tree.
// This is a bit of a hack to tell the two kinds apart.
let categoryTree = catfix.nextElementSibling;
if (
categoryTree &&
(categoryTree.matches(".CategoryTreeTag") || categoryTree.querySelector(".CategoryTreeTag"))
) {
// Skip known empty CategoryTrees
var children = categoryTree.querySelector(".CategoryTreeChildren");
if (children) {
// Process category tree elements already present on the page.
categoryTree.querySelectorAll(".CategoryTreeChildren a").forEach(processLink);
// Also deal with any category tree data loaded dynamically using a MutationObserver.
(new MutationObserver(events => events
.flatMap(event => [...event.addedNodes])
.flatMap(node => [...node.querySelectorAll("a")])
.forEach(processLink)
)).observe(children, {
childList: true,
subtree: true
});
}
} else {
document.querySelectorAll(".mw-category li > a").forEach(processLink);
}
}
// </nowiki>