MediaWiki:Gadget-WantedPagesMainspace.js: Difference between revisions
Appearance
Created page with "// Gadget: WantedPagesMainspace // Filters a transcluded Special:WantedPages list down to mainspace-only, // with a simple blacklist of prefixes for admin/help/template/internal stuff. mw.loader.using(['mediawiki.Title', 'mediawiki.util'], function () { // Only run on the specific portal (adjust if you want Special:WantedPages instead) if (mw.config.get('wgPageName') !== 'Portal:WantedPages') { return; } var container = document.getElementById('..." |
No edit summary |
||
| Line 1: | Line 1: | ||
// Gadget: WantedPagesMainspace | // Gadget: WantedPagesMainspace | ||
// Filters a transcluded Special:WantedPages list down to mainspace-only, | // Filters a transcluded Special:WantedPages list down to mainspace-only, | ||
// with a | // 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 | // 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 14: | ||
} | } | ||
// Prefixes we never want to show here | // Prefixes we never want to show here (text check) | ||
var bannedPrefixes = [ | var bannedPrefixes = [ | ||
'Help:', | 'Help:', | ||
| Line 20: | Line 20: | ||
'The Deep Tech Wiki:', | 'The Deep Tech Wiki:', | ||
'Wikipedia:', | 'Wikipedia:', | ||
'Wikipedia talk:', | |||
'WP:', | 'WP:', | ||
'Wp:', | 'Wp:', | ||
| Line 54: | Line 55: | ||
var links = container.querySelectorAll('a'); | var links = container.querySelectorAll('a'); | ||
var mainspaceLinks = []; | var mainspaceLinks = []; | ||
var seen = Object.create(null); // for deduping by title key | |||
links.forEach(function (link) { | links.forEach(function (link) { | ||
var href = link.getAttribute('href') || ''; | |||
var text = (link.textContent || '').trim(); | var text = (link.textContent || '').trim(); | ||
if (!text) { | if (!text) { | ||
return; | |||
} | |||
// Skip the "222 links" type entries, which point to Special:WhatLinksHere | |||
if (href.indexOf('Special:WhatLinksHere') !== -1) { | |||
return; | return; | ||
} | } | ||
| Line 66: | Line 74: | ||
} | } | ||
// | // Determine the target title; prefer the title attribute if present | ||
var titleObj = mw.Title.newFromText( | var titleText = link.getAttribute('title') || text; | ||
var titleObj = mw.Title.newFromText(titleText); | |||
if (!titleObj) { | if (!titleObj) { | ||
return; | return; | ||
| Line 76: | Line 85: | ||
return; | return; | ||
} | } | ||
// Deduplicate based on normalized DB key | |||
var key = titleObj.getPrefixedDb(); | |||
if (seen[key]) { | |||
return; | |||
} | |||
seen[key] = true; | |||
// Passed all filters; keep a clone of the link | // Passed all filters; keep a clone of the link | ||
Revision as of 09:00, 15 November 2025
// 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 () {
// Only run on the specific portal (adjust if you want a different page)
if (mw.config.get('wgPageName') !== 'Portal:WantedPages') {
return;
}
var container = document.getElementById('wantedpages-mainonly');
if (!container) {
return;
}
// Prefixes we never want to show here (text check)
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) {
for (var i = 0; i < bannedPrefixes.length; i++) {
if (text.indexOf(bannedPrefixes[i]) === 0) {
return true;
}
}
return false;
}
// Optional: exact titles you never want (even if mainspace)
var bannedExactTitles = [
// 'Stats:EN/Sitemap',
// 'Red link example'
];
function isBannedExact(text) {
return bannedExactTitles.indexOf(text) !== -1;
}
// Collect all links produced by the transcluded Special:WantedPages
var links = container.querySelectorAll('a');
var mainspaceLinks = [];
var seen = Object.create(null); // for deduping by title key
links.forEach(function (link) {
var href = link.getAttribute('href') || '';
var text = (link.textContent || '').trim();
if (!text) {
return;
}
// Skip the "222 links" type entries, which point to Special:WhatLinksHere
if (href.indexOf('Special:WhatLinksHere') !== -1) {
return;
}
// Quick blacklist checks on raw text
if (isBannedPrefix(text) || isBannedExact(text)) {
return;
}
// Determine the target title; prefer the title attribute if present
var titleText = link.getAttribute('title') || text;
var titleObj = mw.Title.newFromText(titleText);
if (!titleObj) {
return;
}
// Namespace 0 is the main/article namespace
if (titleObj.getNamespaceId() !== 0) {
return;
}
// Deduplicate based on normalized DB key
var key = titleObj.getPrefixedDb();
if (seen[key]) {
return;
}
seen[key] = true;
// Passed all filters; keep a clone of the link
mainspaceLinks.push(link.cloneNode(true));
});
// Build a new clean <ul> with only accepted links
var ul = document.createElement('ul');
mainspaceLinks.forEach(function (l) {
var li = document.createElement('li');
li.appendChild(l);
ul.appendChild(li);
});
// Replace the original contents with the cleaned list
container.innerHTML = '';
container.appendChild(ul);
});