User:Ahecht/Scripts/searchandcheck.js
Appearance
< User:Ahecht | Scripts
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.
Documentation for this user script can be added at User:Ahecht/Scripts/searchandcheck.
$(document).ready( function()
{
mw.loader.using(["mediawiki.api"]).then(function ()
{
async function searchAndCheck() {
let needle = prompt("Text to search for");
if (!needle) return;
console.log(`Starting scan for "${needle}"...`);
const api = new mw.Api();
const checkedRevisions = [];
const checkboxes = document.querySelectorAll('input[type="checkbox"][name^="ids["]');
const elementsToScan = [];
checkboxes.forEach(cb => {
const nameAttr = cb.name;
const cleanIdStr = nameAttr.replace('ids[', '').replace(']', '');
if (cleanIdStr && !isNaN(cleanIdStr)) {
elementsToScan.push({ id: cleanIdStr, element: cb });
}
});
const revIds = elementsToScan.map(item => item.id);
if (revIds.length === 0) {
console.log("No valid revision checkboxes found on this page.");
return;
}
console.log(`Found ${revIds.length} revisions to inspect on the page.`);
const batchSize = 50;
for (let i = 0; i < revIds.length; i += batchSize) {
const batch = revIds.slice(i, i + batchSize);
try {
const response = await api.get({
action: 'query',
prop: 'revisions',
revids: batch.join('|'),
rvprop: 'content|ids',
rvslots: 'main',
formatversion: 2
});
const pages = response?.query?.pages || [];
for (const page of pages) {
const revisions = page.revisions || [];
for (const rev of revisions) {
if (!rev) {
continue;
} else if (!rev.revid) {
console.log("No revid found in");console.log(rev);
continue;
}
const haystack = rev.slots?.main?.content || rev.content || '';
const revIdStr = rev.revid.toString();
if (haystack.includes(needle)) {
checkedRevisions.push(revIdStr);
}
}
}
} catch (error) {
console.error("Error fetching revision batch:", error);
}
}
let matchCount = 0;
elementsToScan.forEach(item => {
if (checkedRevisions.includes(item.id)) {
item.element.checked = true;
matchCount++;
item.element.dispatchEvent(new Event('change', { bubbles: true }));
}
});
alert(`Scan complete. Successfully checked ${matchCount} revision boxes matching "${needle}".`);
}
document.querySelector('.mw-history-revisionactions')?.insertAdjacentHTML('afterbegin', ' <button type="submit" name="title" id="searchAndCheck" value="Special:RevisionDelete" class="cdx-button" onclick="event.preventDefault();">Check revs based on text</button> ');
document.getElementById('searchAndCheck').addEventListener('click', searchAndCheck);
});
});