Jump to content
This site is currently in alpha, so you will run into rough edges.

MediaWiki:Gadget-WantedPagesMainspace.js: Difference between revisions

From The Deep Tech Wiki
No edit summary
Undo revision 8139 by C (talk)
Tag: Undo
 
(2 intermediate revisions by the same user not shown)
Line 1: Line 1:
// Gadget: WantedPagesMainspace
// Gadget: WantedPagesMainspace
// Filters a transcluded Special:WantedPages list down to mainspace-only,
// with a blacklist of prefixes for admin/help/template/internal stuff.
mw.loader.using(['mediawiki.Title', 'mediawiki.util'], function () {
mw.loader.using(['mediawiki.Title', 'mediawiki.util'], function () {
    // Only run on the specific portal (adjust if you want a different page)
     if (mw.config.get('wgPageName') !== 'Portal:WantedPages') {
     if (mw.config.get('wgPageName') !== 'Portal:WantedPages') {
         return;
         return;
Line 14: Line 10:
     }
     }


    // Prefixes we never want to show here (text check)
     var bannedPrefixes = [
     var bannedPrefixes = [
         'Help:',
         'Help:',
Line 34: Line 29:


     function isBannedPrefix(text) {
     function isBannedPrefix(text) {
         for (var i = 0; i < bannedPrefixes.length; i++) {
         return bannedPrefixes.some(function (p) {
             if (text.indexOf(bannedPrefixes[i]) === 0) {
             return text.startsWith(p);
                return true;
         });
            }
         }
        return false;
     }
     }


    // Optional: exact titles you never want (even if mainspace)
     var bannedExactTitles = [];
     var bannedExactTitles = [
        // 'Stats:EN/Sitemap',
        // 'Red link example'
    ];


     function isBannedExact(text) {
     function isBannedExact(text) {
Line 52: Line 40:
     }
     }


    // Collect all links produced by the transcluded Special:WantedPages
     var links = container.querySelectorAll('a');
     var links = container.querySelectorAll('a');
    var mainspaceLinks = [];
     var seen = Object.create(null);
     var seen = Object.create(null); // for deduping by title key
    var cleanedItems = [];


     links.forEach(function (link) {
     links.forEach(function (link) {
         var href = link.getAttribute('href') || '';
         var href = link.getAttribute('href') || '';
         var text = (link.textContent || '').trim();
         var text = (link.textContent || '').trim();
         if (!text) {
         if (!text) return;
            return;
        }


         // Skip the "222 links" type entries, which point to Special:WhatLinksHere
         // Skip "### links" (they always point to Special:WhatLinksHere)
         if (href.indexOf('Special:WhatLinksHere') !== -1) {
         if (href.includes('Special:WhatLinksHere')) return;
            return;
        }


        // Quick blacklist checks on raw text
         if (isBannedPrefix(text) || isBannedExact(text)) return;
         if (isBannedPrefix(text) || isBannedExact(text)) {
            return;
        }


        // Determine the target title; prefer the title attribute if present
         var titleText = link.getAttribute('title') || text;
         var titleText = link.getAttribute('title') || text;
         var titleObj = mw.Title.newFromText(titleText);
         var titleObj = mw.Title.newFromText(titleText);
         if (!titleObj) {
         if (!titleObj) return;
            return;
        }


        // Namespace 0 is the main/article namespace
         if (titleObj.getNamespaceId() !== 0) return;
         if (titleObj.getNamespaceId() !== 0) {
            return;
        }


        // Deduplicate based on normalized DB key
         var key = titleObj.getPrefixedDb();
         var key = titleObj.getPrefixedDb();
         if (seen[key]) {
         if (seen[key]) return;
            return;
        }
         seen[key] = true;
         seen[key] = true;


         // Passed all filters; keep a clone of the link
         cleanedItems.push(link.cloneNode(true));
        mainspaceLinks.push(link.cloneNode(true));
     });
     });


     // Build a new clean <ul> with only accepted links
     // Build a new <ul> only with real content
     var ul = document.createElement('ul');
     var ul = document.createElement('ul');
     mainspaceLinks.forEach(function (l) {
 
     cleanedItems.forEach(function (l) {
         var li = document.createElement('li');
         var li = document.createElement('li');
         li.appendChild(l);
         li.appendChild(l);
Line 105: Line 76:
     });
     });


     // Replace the original contents with the cleaned list
     // Replace old messy HTML
     container.innerHTML = '';
     container.innerHTML = '';
     container.appendChild(ul);
     container.appendChild(ul);
});
});

Latest revision as of 09:50, 16 November 2025

// Gadget: WantedPagesMainspace
mw.loader.using(['mediawiki.Title', 'mediawiki.util'], function () {
    if (mw.config.get('wgPageName') !== 'Portal:WantedPages') {
        return;
    }

    var container = document.getElementById('wantedpages-mainonly');
    if (!container) {
        return;
    }

    var bannedPrefixes = [
        'Help:',
        'Template:',
        'The Deep Tech Wiki:',
        'Wikipedia:',
        'Wikipedia talk:',
        'WP:',
        'Wp:',
        'Project:',
        'Category:',
        'File:',
        'Module:',
        'MediaWiki:',
        'User:',
        'User talk:',
        'Talk:'
    ];

    function isBannedPrefix(text) {
        return bannedPrefixes.some(function (p) {
            return text.startsWith(p);
        });
    }

    var bannedExactTitles = [];

    function isBannedExact(text) {
        return bannedExactTitles.indexOf(text) !== -1;
    }

    var links = container.querySelectorAll('a');
    var seen = Object.create(null);
    var cleanedItems = [];

    links.forEach(function (link) {
        var href = link.getAttribute('href') || '';
        var text = (link.textContent || '').trim();
        if (!text) return;

        // Skip "### links" (they always point to Special:WhatLinksHere)
        if (href.includes('Special:WhatLinksHere')) return;

        if (isBannedPrefix(text) || isBannedExact(text)) return;

        var titleText = link.getAttribute('title') || text;
        var titleObj = mw.Title.newFromText(titleText);
        if (!titleObj) return;

        if (titleObj.getNamespaceId() !== 0) return;

        var key = titleObj.getPrefixedDb();
        if (seen[key]) return;
        seen[key] = true;

        cleanedItems.push(link.cloneNode(true));
    });

    // Build a new <ul> only with real content
    var ul = document.createElement('ul');

    cleanedItems.forEach(function (l) {
        var li = document.createElement('li');
        li.appendChild(l);
        ul.appendChild(li);
    });

    // Replace old messy HTML
    container.innerHTML = '';
    container.appendChild(ul);
});