Content deleted Content added
Polygnotus (talk | contribs) ←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...' |
Polygnotus (talk | contribs) 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}}▼
//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('[
}
}
if ((mw.config.get('wgNamespaceNumber') !== 0 && mw.config.get('wgPageName') !== 'User:Polygnotus/
mw.config.get('wgAction') !== 'view' ||
!mw.config.get('wgIsProbablyEditable')) {
Line 25 ⟶ 27:
function replaceVerifySpellingTemplates() {
try {
const verifyElements = document.
debug(`Found ${verifyElements.length} verify elements`);
let templateInstances = {};
Line 31 ⟶ 33:
Array.from(verifyElements).forEach((element, index) => {
try {
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;
}
▲ 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
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(
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(
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;
});
|