MediaWiki:Gadget-WantedPagesMainspace.js: Difference between revisions
No edit summary |
Tag: Undo |
||
| (2 intermediate revisions by the same user not shown) | |||
| Line 1: | Line 1: | ||
// Gadget: WantedPagesMainspace | // Gadget: WantedPagesMainspace | ||
mw.loader.using(['mediawiki.Title', 'mediawiki.util'], function () { | mw.loader.using(['mediawiki.Title', 'mediawiki.util'], function () { | ||
if (mw.config.get('wgPageName') !== 'Portal:WantedPages') { | if (mw.config.get('wgPageName') !== 'Portal:WantedPages') { | ||
return; | return; | ||
| Line 14: | Line 10: | ||
} | } | ||
var bannedPrefixes = [ | var bannedPrefixes = [ | ||
'Help:', | 'Help:', | ||
| Line 34: | Line 29: | ||
function isBannedPrefix(text) { | function isBannedPrefix(text) { | ||
return bannedPrefixes.some(function (p) { | |||
return text.startsWith(p); | |||
}); | |||
} | |||
} | } | ||
var bannedExactTitles = []; | |||
var bannedExactTitles = [ | |||
function isBannedExact(text) { | function isBannedExact(text) { | ||
| Line 52: | Line 40: | ||
} | } | ||
var links = container.querySelectorAll('a'); | var links = container.querySelectorAll('a'); | ||
var seen = Object.create(null); | |||
var seen = Object.create(null); | 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; | ||
// Skip | // Skip "### links" (they always point to Special:WhatLinksHere) | ||
if (href. | if (href.includes('Special:WhatLinksHere')) return; | ||
if (isBannedPrefix(text) || isBannedExact(text)) return; | |||
if (isBannedPrefix(text) || isBannedExact(text)) | |||
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; | ||
if (titleObj.getNamespaceId() !== 0) return; | |||
if (titleObj.getNamespaceId() !== 0) | |||
var key = titleObj.getPrefixedDb(); | var key = titleObj.getPrefixedDb(); | ||
if (seen[key]) | if (seen[key]) return; | ||
seen[key] = true; | seen[key] = true; | ||
cleanedItems.push(link.cloneNode(true)); | |||
}); | }); | ||
// Build a new | // Build a new <ul> only with real content | ||
var ul = document.createElement('ul'); | var ul = document.createElement('ul'); | ||
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 | // Replace old messy HTML | ||
container.innerHTML = ''; | container.innerHTML = ''; | ||
container.appendChild(ul); | container.appendChild(ul); | ||
}); | }); | ||