User:Polygnotus/typo.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. |
Documentation for this user script can be added at User:Polygnotus/typo. |
// https://en.wikipedia.org/w/index.php?title=Giraffe&action=edit&typo=the&typofix=teh
// <nowiki>
$(document).ready(function() {
function getUrlParameter(name) {
name = name.replace(/[\[]/, '\\[').replace(/[\]]/, '\\]');
var regex = new RegExp('[\\?&]' + name + '=([^&#]*)');
var results = regex.exec(location.search);
return results === null ? '' : decodeURIComponent(results[1].replace(/\+/g, ' '));
}
function replaceTextInWikitextEditor(typo, typofix) {
var editTextarea = document.getElementById('wpTextbox1');
if (editTextarea) {
var currentText = editTextarea.textContent;
// Match the typo in a case insensitive manner
//var typoPattern = new RegExp('\\b' + typo + '\\b', 'gi');
//not so sure i should use word boundaries
//gi = global+caseinsensitive
var typoPattern = new RegExp(typo, 'gi');
var newText = currentText.replace(typoPattern, function(match) {
if (match.charAt(0) === match.charAt(0).toUpperCase()) {
return typofix.charAt(0).toUpperCase() + typofix.slice(1);
} else {
return typofix.toLowerCase();
}
});
editTextarea.textContent = newText;
if (editTextarea.textContent.includes(typofix)) {
// Fill in the edit summary before clicking the "Show changes" button
var changeSummary = typo + ' → ' + typofix;
var editSummaryField = document.getElementById('wpSummary');
if (editSummaryField) {
editSummaryField.value = changeSummary;
}
var showChangesButton = document.querySelector('input[name="wpDiff"]');
if (showChangesButton) {
showChangesButton.click();
}
} else {
console.log("Replacement text not found in the edit textarea.");
}
}
}
var typo = getUrlParameter('typo');
var typofix = getUrlParameter('typofix');
if (typo && typofix && typo !== '' && typofix !== '') {
replaceTextInWikitextEditor(typo, typofix);
}
});
// </nowiki>