User:SoledadKabocha/antiBracketBot.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:SoledadKabocha/antiBracketBot. |
window.checkEditBrackets = function ( ) {
var editbox = $( '#wpTextbox1' );
if ( !editbox || editbox.length === 0 ) {
//TODO: log
return;
}
var editboxtext = editbox.val( );
if ( typeof editboxtext !== 'string' ) {
//TODO: log
return;
}
// not to be used in mainspace articles, but should be generally harmless
editboxtext = editboxtext.replace( /<!--noChkBracket-->.*<!--\/noChkBracket-->/g, '' );
var warningStr = '';
var initialCountDone = ( typeof window.aBBParenBalance === 'number' && typeof window.aBBBraceBalance === 'number'
&& typeof window.aBBSqrbkBalance === 'number' && typeof window.aBBAngleBalance === 'number' );
var parenBalance = (editboxtext.match(/\(/g)||[]).length - (editboxtext.match(/\)/g)||[]).length;
if ( !initialCountDone ) {
window.aBBParenBalance = parenBalance;
}
if ( parenBalance !== 0 && ( ( initialCountDone && window.aBBParenBalance !== parenBalance ) || window.aBBWarnInitially ) ) {
//XXX: Should these spans also have IDs?
warningStr = '<span class="antibracketbot-paren">(' + window.aBBParenBalance;
if ( window.aBBParenBalance !== parenBalance ) {
warningStr += ' → ' + parenBalance;
}
warningStr += ')</span>';
}
var braceBalance = (editboxtext.match(/\{/g)||[]).length - (editboxtext.match(/\}/g)||[]).length;
if ( !initialCountDone ) {
window.aBBBraceBalance = braceBalance;
}
if ( braceBalance !== 0 && ( ( initialCountDone && window.aBBBraceBalance !== braceBalance ) || window.aBBWarnInitially ) ) {
warningStr += '<br /><span class="antibracketbot-brace">{' + window.aBBBraceBalance;
if ( window.aBBBraceBalance !== braceBalance ) {
warningStr += ' → ' + braceBalance;
}
warningStr += '}</span>';
}
var sqrbkBalance = (editboxtext.match(/\[/g)||[]).length - (editboxtext.match(/\]/g)||[]).length;
if ( !initialCountDone ) {
window.aBBSqrbkBalance = sqrbkBalance;
}
if ( sqrbkBalance !== 0 && ( ( initialCountDone && window.aBBSqrbkBalance !== sqrbkBalance ) || window.aBBWarnInitially ) ) {
warningStr += '<br /><span class="antibracketbot-sqrbk">[' + window.aBBSqrbkBalance;
if ( window.aBBSqrbkBalance !== sqrbkBalance ) {
warningStr += ' → ' + sqrbkBalance;
}
warningStr += ']</span>';
}
var angleBalance = (editboxtext.match(/</g)||[]).length - (editboxtext.match(/>/g)||[]).length;
if ( !initialCountDone ) {
window.aBBAngleBalance = angleBalance;
}
if ( angleBalance !== 0 && ( ( initialCountDone && window.aBBAngleBalance !== angleBalance ) || window.aBBWarnInitially ) ) {
warningStr += '<br /><span class="antibracketbot-angle"><' + window.aBBAngleBalance;
if ( window.aBBAngleBalance !== angleBalance ) {
warningStr += ' → ' + angleBalance;
}
warningStr += '></span>';
}
if ( warningStr.indexOf( '<br />' ) === 0 ) warningStr = warningStr.substr( 6 );
if ( warningStr ) {
warningStr = '<span class="antibracketbot-all">' + warningStr + '</span>';
mw.notify( $( warningStr ) );
}
}
if ( mw.config.get( 'wgAction' ) === 'edit' || mw.config.get( 'wgAction' ) === 'submit' ) {
//needs to run on page load to store the initial counts to window.aBB(foo)Balance
$( checkEditBrackets );
}