MediaWiki:Common.js: Difference between revisions
No edit summary |
No edit summary |
||
| Line 1: | Line 1: | ||
mw.loader.using('mediawiki.util').then(function () { | mw.loader.using('mediawiki.util').then(function () { | ||
function | function cleanCategoryLinks(root = document) { | ||
root.querySelectorAll('a[href*="/Category:"]').forEach(function(link) { | |||
if (link.dataset.cleaned) return; // avoid double-processing | |||
if (link.textContent.startsWith('Category:')) { | if (link.textContent.startsWith('Category:')) { | ||
link.textContent = link.textContent.replace(/^Category:/, ''); | link.textContent = link.textContent.replace(/^Category:/, ''); | ||
link.dataset.cleaned = true; | |||
} | } | ||
}); | }); | ||
} | } | ||
// | // Initial run | ||
cleanCategoryLinks(); | |||
// | // Observe the page for dynamically added content (MinervaNeue does this) | ||
const observer = new MutationObserver(function(mutations) { | |||
mutations.forEach(function(mutation) { | |||
mutation.addedNodes.forEach(function(node) { | |||
if (node.nodeType === Node.ELEMENT_NODE) { | |||
cleanCategoryLinks(node); | |||
} | |||
}); | |||
}); | |||
}); | |||
observer.observe(document.body, { childList: true, subtree: true }); | |||
// Hide "Category:" in page headers / namespace labels | |||
document.querySelectorAll(' | const hideNamespace = () => { | ||
document.querySelectorAll('.minerva-header .ns-category, .mw-page-title-namespace, .page-heading .ns-category') | |||
.forEach(el => el.style.display = 'none'); | |||
}; | |||
hideNamespace(); | |||
// | // Also observe for header changes | ||
const headerObserver = new MutationObserver(hideNamespace); | |||
const header = document.querySelector('body'); | |||
if (header) headerObserver.observe(header, { childList: true, subtree: true }); | |||
}); | }); | ||
Revision as of 03:40, 9 October 2025
mw.loader.using('mediawiki.util').then(function () {
function cleanCategoryLinks(root = document) {
root.querySelectorAll('a[href*="/Category:"]').forEach(function(link) {
if (link.dataset.cleaned) return; // avoid double-processing
if (link.textContent.startsWith('Category:')) {
link.textContent = link.textContent.replace(/^Category:/, '');
link.dataset.cleaned = true;
}
});
}
// Initial run
cleanCategoryLinks();
// Observe the page for dynamically added content (MinervaNeue does this)
const observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
mutation.addedNodes.forEach(function(node) {
if (node.nodeType === Node.ELEMENT_NODE) {
cleanCategoryLinks(node);
}
});
});
});
observer.observe(document.body, { childList: true, subtree: true });
// Hide "Category:" in page headers / namespace labels
const hideNamespace = () => {
document.querySelectorAll('.minerva-header .ns-category, .mw-page-title-namespace, .page-heading .ns-category')
.forEach(el => el.style.display = 'none');
};
hideNamespace();
// Also observe for header changes
const headerObserver = new MutationObserver(hideNamespace);
const header = document.querySelector('body');
if (header) headerObserver.observe(header, { childList: true, subtree: true });
});