Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page.
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
// <nowiki>// ==UserScript==// @name Wikipedia Namespace Filter// @version 0.4// @description Filter special page results by namespace, including mainspace, with All/None options and storing prefs in localstorage// @match https://*.wikipedia.org/w/index.php?title=Special:LinkSearch*// @grant none// ==/UserScript==(function(){'use strict';constSTORAGE_KEY='wikipediaNamespaceFilter';functionaddStyles(){conststyle=document.createElement('style');style.textContent=` #namespace-filter { margin: 10px 0; padding: 10px; border: 1px solid #a2a9b1; background-color: #f8f9fa; } #namespace-filter label { margin-right: 10px; display: inline-block; } #all-none-options { margin-bottom: 10px; } #all-none-options button { margin-right: 10px; } `;document.head.appendChild(style);}functiongetNamespaces(){constnamespaces=newSet(['Main']);document.querySelectorAll('ol.special li a:nth-child(2)').forEach(link=>{consttext=link.textContent;constcolonIndex=text.indexOf(':');if(colonIndex===-1){namespaces.add('Main');}else{constnamespace=text.substring(0,colonIndex);if(namespace){namespaces.add(namespace);}}});returnArray.from(namespaces).sort((a,b)=>a==='Main'?-1:b==='Main'?1:a.localeCompare(b));}functioncreateFilterUI(namespaces){constfilterDiv=document.createElement('div');filterDiv.id='namespace-filter';filterDiv.innerHTML='<h4>Filter by namespace:</h4>';// Add All/None optionsconstallNoneDiv=document.createElement('div');allNoneDiv.id='all-none-options';constallButton=document.createElement('button');allButton.textContent='Select All';allButton.addEventListener('click',()=>setAllCheckboxes(true));constnoneButton=document.createElement('button');noneButton.textContent='Select None';noneButton.addEventListener('click',()=>setAllCheckboxes(false));allNoneDiv.appendChild(allButton);allNoneDiv.appendChild(noneButton);filterDiv.appendChild(allNoneDiv);constsavedFilters=getSavedFilters();namespaces.forEach(namespace=>{constlabel=document.createElement('label');constcheckbox=document.createElement('input');checkbox.type='checkbox';checkbox.value=namespace;checkbox.checked=savedFilters?savedFilters.includes(namespace):true;checkbox.addEventListener('change',()=>{filterResults();saveFilters();});label.appendChild(checkbox);label.appendChild(document.createTextNode(namespace));filterDiv.appendChild(label);});constol=document.querySelector('ol.special');ol.parentNode.insertBefore(filterDiv,ol);}functionsetAllCheckboxes(checked){document.querySelectorAll('#namespace-filter input[type="checkbox"]').forEach(cb=>{cb.checked=checked;});filterResults();saveFilters();}functionfilterResults(){constcheckedNamespaces=Array.from(document.querySelectorAll('#namespace-filter input:checked')).map(cb=>cb.value);document.querySelectorAll('ol.special li').forEach(li=>{constlink=li.querySelector('a:nth-child(2)');consttext=link.textContent;constcolonIndex=text.indexOf(':');constnamespace=colonIndex===-1?'Main':text.substring(0,colonIndex);if(checkedNamespaces.includes(namespace)){li.style.display='';}else{li.style.display='none';}});}functionsaveFilters(){constcheckedNamespaces=Array.from(document.querySelectorAll('#namespace-filter input:checked')).map(cb=>cb.value);localStorage.setItem(STORAGE_KEY,JSON.stringify(checkedNamespaces));}functiongetSavedFilters(){constsavedFilters=localStorage.getItem(STORAGE_KEY);returnsavedFilters?JSON.parse(savedFilters):null;}functioninit(){constnamespaces=getNamespaces();if(namespaces.length>0){addStyles();createFilterUI(namespaces);filterResults();// Apply filters on initial load}}// Run the script when the page is loadedif(document.readyState==='loading'){document.addEventListener('DOMContentLoaded',init);}else{init();}})();// </nowiki>