User:Lupo/autocorrect.js
Jump to navigation
Jump to search
Note: After saving, you have to bypass your browser's cache to see the changes. Internet Explorer: press Ctrl-F5, Mozilla: hold down Shift while clicking Reload (or press Ctrl-Shift-R), Opera/Konqueror: press F5, Safari: hold down Shift + Alt while clicking Reload, Chrome: hold down Shift while clicking Reload.
Documentation for this user script can be added at User:Lupo/autocorrect. |
// <source lang="javascript">
/*
Auto-correct some common (and not-so-common, but obvious) Wikitext errors upon submit
Author: [[User:Lupo]], August 2008
License: Quadruple licensed GFDL, GPL, LGPL and Creative Commons Attribution 3.0 (CC-BY-3.0)
Choose whichever license of these you like best :-)
*/
(function(jQuery){
function fixCategoryTransclusion (field) {
field.value = field.value.replace (/(\{\{)\s*(:?\s*[Cc]ategory\s*:[^|}]*(\|[^}]*)?)(\}\})/g, "[[$2]]");
}
function fixCommonTypos (field) {
field.value = field.value
.replace (/\b([Tt])eh\b/g, "$1he")
.replace (/([Cc])oypright/g, "$1opyright")
.replace (/([Cc])ategroy/g, "$1ategory")
.replace (/(https?:\/\/\w+\.blogspot)\.\w+\//g, "$1.com/")
;
}
function fixField (field) {
// Remember scroll position
var scroll_pos = 0;
if (typeof (field.scrollTop) !== 'undefined')
scroll_pos = field.scrollTop;
else if (document.documentElement && document.documentElement.scrollTop)
scroll_pos = document.documentElement.scrollTop;
else if (document.body)
scroll_pos = document.body.scrollTop;
// Modify field contents
fixCommonTypos (field);
fixCategoryTransclusion (field);
// Others here...
// Restore scroll position
if (typeof (field.scrollTop) !== 'undefined')
field.scrollTop = scroll_pos;
else if (document.documentElement && document.documentElement.scrollTop)
document.documentElement.scrollTop = scroll_pos;
else if (document.body)
document.body.scrollTop = scroll_pos;
}
function setup () {
var form = document.editform;
if (!form) return;
form.onsubmit = (function(old_onsubmit) {
return function() {
var do_submit = true;
// Call previous onsubmit handler, if any
if (old_onsubmit) {
if (typeof (old_onsubmit) === 'string')
do_submit = eval (old_onsubmit);
else if (typeof (old_onsubmit) === 'function')
do_submit = old_onsubmit (e);
}
if (!do_submit) return false;
// Go through all textareas and fix them
for (var i = 0; i < form.elements.length; i++) {
var element = form.elements[i];
if (element.nodeName.toLowerCase () === 'form') continue; // Don't do nested forms.
if (element.type === 'textarea') fixField (element);
}
return true;
};
})(form.onsubmit);
}
jQuery(document).ready(setup);
})(window.jQuery);
// </source>