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 removeCategoryPrefixFromNode(node) { | |||
if (link.dataset.cleaned) return; | // If the node itself is a link or contains links | ||
if (link.textContent.startsWith('Category:')) { | const links = node.querySelectorAll ? node.querySelectorAll('a') : []; | ||
links.forEach(function(link) { | |||
// Skip if already processed | |||
if (link.dataset.cleaned) return; | |||
if (link.href && link.href.includes('/Category:') && link.textContent.startsWith('Category:')) { | |||
link.textContent = link.textContent.replace(/^Category:/, ''); | link.textContent = link.textContent.replace(/^Category:/, ''); | ||
link.dataset.cleaned = true; | link.dataset.cleaned = true; | ||
} | } | ||
}); | }); | ||
// Additionally, check if the node itself is a link | |||
if (node.nodeType === Node.ELEMENT_NODE && node.tagName === 'A' && | |||
node.href.includes('/Category:') && node.textContent.startsWith('Category:')) { | |||
node.textContent = node.textContent.replace(/^Category:/, ''); | |||
node.dataset.cleaned = true; | |||
} | |||
} | |||
function cleanCategoryLinks(root = document) { | |||
removeCategoryPrefixFromNode(root); | |||
if (root.querySelectorAll) { | |||
root.querySelectorAll('a[href*="/Category:"]').forEach(removeCategoryPrefixFromNode); | |||
} | |||
} | } | ||
// Initial | // Initial clean | ||
cleanCategoryLinks(); | cleanCategoryLinks(); | ||
// Observe the | // Observe the body for dynamically added content | ||
const observer = new MutationObserver(function(mutations) { | const observer = new MutationObserver(function(mutations) { | ||
mutations.forEach(function(mutation) { | mutations.forEach(function(mutation) { | ||
mutation.addedNodes.forEach( | mutation.addedNodes.forEach(cleanCategoryLinks); | ||
}); | }); | ||
}); | }); | ||
| Line 26: | Line 40: | ||
observer.observe(document.body, { childList: true, subtree: true }); | observer.observe(document.body, { childList: true, subtree: true }); | ||
// Hide "Category:" in | // Hide "Category:" in headers | ||
const hideNamespace = () => { | const hideNamespace = () => { | ||
document.querySelectorAll('.minerva-header .ns-category, .mw-page-title-namespace, .page-heading .ns-category') | document.querySelectorAll('.minerva-header .ns-category, .mw-page-title-namespace, .page-heading .ns-category') | ||
.forEach(el => el.style.display = 'none'); | |||
}; | }; | ||
hideNamespace(); | hideNamespace(); | ||
// | // Observe headers too | ||
const headerObserver = new MutationObserver(hideNamespace); | const headerObserver = new MutationObserver(hideNamespace); | ||
headerObserver.observe(document.body, { childList: true, subtree: true }); | |||
}); | }); | ||
Revision as of 03:41, 9 October 2025
mw.loader.using('mediawiki.util').then(function () {
function removeCategoryPrefixFromNode(node) {
// If the node itself is a link or contains links
const links = node.querySelectorAll ? node.querySelectorAll('a') : [];
links.forEach(function(link) {
// Skip if already processed
if (link.dataset.cleaned) return;
if (link.href && link.href.includes('/Category:') && link.textContent.startsWith('Category:')) {
link.textContent = link.textContent.replace(/^Category:/, '');
link.dataset.cleaned = true;
}
});
// Additionally, check if the node itself is a link
if (node.nodeType === Node.ELEMENT_NODE && node.tagName === 'A' &&
node.href.includes('/Category:') && node.textContent.startsWith('Category:')) {
node.textContent = node.textContent.replace(/^Category:/, '');
node.dataset.cleaned = true;
}
}
function cleanCategoryLinks(root = document) {
removeCategoryPrefixFromNode(root);
if (root.querySelectorAll) {
root.querySelectorAll('a[href*="/Category:"]').forEach(removeCategoryPrefixFromNode);
}
}
// Initial clean
cleanCategoryLinks();
// Observe the body for dynamically added content
const observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
mutation.addedNodes.forEach(cleanCategoryLinks);
});
});
observer.observe(document.body, { childList: true, subtree: true });
// Hide "Category:" in headers
const hideNamespace = () => {
document.querySelectorAll('.minerva-header .ns-category, .mw-page-title-namespace, .page-heading .ns-category')
.forEach(el => el.style.display = 'none');
};
hideNamespace();
// Observe headers too
const headerObserver = new MutationObserver(hideNamespace);
headerObserver.observe(document.body, { childList: true, subtree: true });
});