No edit summary
No edit summary
Line 1: Line 1:
/* Any JavaScript here will be loaded for all users on every page load. */
mw.loader.using('mediawiki.util').then(function () {
mw.loader.using('mediawiki.util').then(function () {
     function removeCategoryPrefix() {
     function cleanCategoryLinks(root = document) {
        // Remove prefix from page title
         root.querySelectorAll('a[href*="/Category:"]').forEach(function(link) {
        const ns = document.querySelector('.minerva-header .ns-category, .mw-page-title-namespace');
            if (link.dataset.cleaned) return; // avoid double-processing
        if (ns) ns.style.display = 'none';
 
        // Remove prefix from category page heading
        const heading = document.querySelector('.page-heading .ns-category');
        if (heading) heading.style.display = 'none';
 
        // Remove prefix from category links in content
         document.querySelectorAll('a[href*="/Category:"]').forEach(function (link) {
             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;
             }
             }
         });
         });
     }
     }


     // Run when page first loads
     // Initial run
     removeCategoryPrefix();
     cleanCategoryLinks();


     // Run again after mobile content loads dynamically (Minerva does this)
     // Observe the page for dynamically added content (MinervaNeue does this)
     mw.hook('wikipage.content').add(removeCategoryPrefix);
     const observer = new MutationObserver(function(mutations) {
});
        mutations.forEach(function(mutation) {
 
            mutation.addedNodes.forEach(function(node) {
mw.loader.using('mediawiki.util').then(function () {
                if (node.nodeType === Node.ELEMENT_NODE) {
    function removeCategoryPrefixes() {
                    cleanCategoryLinks(node);
        // 1️⃣ Hide "Category:" in page title / header
                }
         const ns = document.querySelector('.minerva-header .ns-category, .mw-page-title-namespace');
            });
        if (ns) ns.style.display = 'none';
         });
    });


        const heading = document.querySelector('.page-heading .ns-category');
    observer.observe(document.body, { childList: true, subtree: true });
        if (heading) heading.style.display = 'none';


        // 2️⃣ Remove "Category:" from ALL visible link texts (articles + categories)
    // Hide "Category:" in page headers / namespace labels
         document.querySelectorAll('a[href*="/Category:"]').forEach(function (link) {
    const hideNamespace = () => {
            const txt = link.textContent.trim();
         document.querySelectorAll('.minerva-header .ns-category, .mw-page-title-namespace, .page-heading .ns-category')
            if (txt.startsWith('Category:')) {
                 .forEach(el => el.style.display = 'none');
                 link.textContent = txt.replace(/^Category:/, '');
    };
            }
        });


        // 3️⃣ Handle dynamically loaded Minerva content (mobile)
    hideNamespace();
        mw.hook('wikipage.content').add(function () {
            document.querySelectorAll('a[href*="/Category:"]').forEach(function (link) {
                const txt = link.textContent.trim();
                if (txt.startsWith('Category:')) {
                    link.textContent = txt.replace(/^Category:/, '');
                }
            });
        });
    }


     // Run on load and when new content is inserted
     // Also observe for header changes
     removeCategoryPrefixes();
    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 });
});