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 cleanCategoryLinks(root = document) {
 
         root.querySelectorAll('a[href*="/Category:"]').forEach(function(link) {
     function removeCategoryPrefixFromNode(node) {
             if (link.dataset.cleaned) return; // avoid double-processing
         // 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 run
     // Initial clean
     cleanCategoryLinks();
     cleanCategoryLinks();


     // Observe the page for dynamically added content (MinervaNeue does this)
     // 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(function(node) {
             mutation.addedNodes.forEach(cleanCategoryLinks);
                if (node.nodeType === Node.ELEMENT_NODE) {
                    cleanCategoryLinks(node);
                }
            });
         });
         });
     });
     });
Line 26: Line 40:
     observer.observe(document.body, { childList: true, subtree: true });
     observer.observe(document.body, { childList: true, subtree: true });


     // Hide "Category:" in page headers / namespace labels
     // 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');
            .forEach(el => el.style.display = 'none');
     };
     };
     hideNamespace();
     hideNamespace();


     // Also observe for header changes
     // Observe headers too
     const headerObserver = new MutationObserver(hideNamespace);
     const headerObserver = new MutationObserver(hideNamespace);
     const header = document.querySelector('body');
     headerObserver.observe(document.body, { childList: true, subtree: true });
    if (header) headerObserver.observe(header, { 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 });

});