User:Polygnotus/Scripts/TypoFixer.js: Difference between revisions

Content deleted Content added
Created page with '// Example: tyop{{verify spelling|reason=tyop=>typo}} //I am using the reason parameter because there is no suggestion parameter (yet) //each of these templates is given an id so we can know that they clicked on the nth occurance. // <nowiki> mw.loader.using(['mediawiki.util'], function () { $(document).ready(function () { 'use strict'; const DEBUG = true; const DEBUG_DELAY = 0; // in ms. It can be useful to have a dela...'
 
No edit summary
 
(4 intermediate revisions by the same user not shown)
Line 1:
//Testpage: https://en.wikipedia.org/wiki/User:Polygnotus/TypoFixerTest
// Example: tyop{{verify spelling|reason=tyop=>typo}}
 
// Example: tyop{{verify spelling|reason=tyop=>typo}}
//I am using the reason parameter because there is no suggestion parameter (yet)
//each of these templates is given an id so we can know that they clicked on the nth occurance.
Line 12 ⟶ 14:
function debug(...args) {
if (DEBUG) {
console.log('[VerifySpellingTypoFixer]', ...args);
}
}
 
if ((mw.config.get('wgNamespaceNumber') !== 0 && mw.config.get('wgPageName') !== 'User:Polygnotus/verifytestScripts/TypoFixerTest') ||
mw.config.get('wgAction') !== 'view' ||
!mw.config.get('wgIsProbablyEditable')) {
Line 25 ⟶ 27:
function replaceVerifySpellingTemplates() {
try {
const verifyElements = document.getElementsByClassNamequerySelectorAll('.noprint .Inline-Template, .noprint.Template-Fact');
debug(`Found ${verifyElements.length} verify elements`);
let templateInstances = {};
Line 31 ⟶ 33:
Array.from(verifyElements).forEach((element, index) => {
try {
constlet spanElement, =from, element.querySelector('span[title]')to;
if (!spanElement) {
debugif (`No span element.classList.contains('Inline-Template')) found in verify element ${index}`);
returnspanElement = element.querySelector('span[title]');
const [from, to] = if (!spanElement || !spanElement.title.splitincludes('=>').map(s) => s.trim());{
debug(`Invalid titleinline template format in verify element ${index}`);
return;
}
[from, to] = spanElement.title.split('=>').map(s => s.trim());
} else if (element.classList.contains('Template-Fact')) {
const templateContent = element.textContent;
const match = templateContent.match(/reason=([^=>\s]+)\s*=>\s*([^=>\s]+)/);
if (!match) {
debug(`Invalid block template format in element ${index}`);
return;
}
[, from, to] = match;
}
if (!spanElement.title.includes('=>')) {
debug(`Invalid title format in verify element ${index}`);
return;
}
const [from, to] = spanElement.title.split('=>').map(s => s.trim());
debug(`Processing element: ${from} => ${to}`);
Line 102 ⟶ 113:
debug('Original content:', content);
 
const templateRegexinlineTemplateRegex = new RegExp(`(${escapeRegExp(from)})\\s*{{\\s*verify\\s+spelling\\s*(?:\\|[^}]*)?}}`, 'g');
const blockTemplateRegex = new RegExp(`(${escapeRegExp(from)})\\s*{{\\s*verify\\s+spelling\\s*\\|\\s*reason\\s*=\\s*${escapeRegExp(from)}\\s*=>\\s*${escapeRegExp(to)}\\s*}}`, 'g');
let count = 0;
if (action === 'fix') {
content = content.replace(templateRegexinlineTemplateRegex, (match, p1) => {
count++;
debug(`Matched inline instance ${count} of ${from}`);
return count === parseInt(instanceNumber) ? to : match;
});
content = content.replace(blockTemplateRegex, (match, p1) => {
count++;
debug(`Matched block instance ${count} of ${from}`);
return count === parseInt(instanceNumber) ? to : match;
});
Line 115 ⟶ 132:
summary = `Fixing spelling: ${from} → ${to} and removing verification template (#${instanceNumber})`;
} else if (action === 'remove') {
content = content.replace(templateRegexinlineTemplateRegex, (match, p1) => {
count++;
debug(`Matched inline #${count} of ${from} for removal`);
return count === parseInt(instanceNumber) ? p1 : match;
});
content = content.replace(blockTemplateRegex, (match, p1) => {
count++;
debug(`Matched block #${count} of ${from} for removal`);
return count === parseInt(instanceNumber) ? p1 : match;
});