User:SoledadKabocha/antiBracketBot.js

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( /.*/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 = ' (' + window.aBBParenBalance;		if ( window.aBBParenBalance !== parenBalance ) {			warningStr += ' &rarr; ' + parenBalance;		}		warningStr += ') '; }

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 += ' {' + window.aBBBraceBalance; if ( window.aBBBraceBalance !== braceBalance ) { warningStr += ' &rarr; ' + braceBalance; }		warningStr += '} '; }

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 += ' [' + window.aBBSqrbkBalance; if ( window.aBBSqrbkBalance !== sqrbkBalance ) { warningStr += ' &rarr; ' + sqrbkBalance; }		warningStr += '] '; }

var angleBalance = (editboxtext.match(//g)||[]).length; if ( !initialCountDone ) { window.aBBAngleBalance = angleBalance; }	if ( angleBalance !== 0 && ( ( initialCountDone && window.aBBAngleBalance !== angleBalance ) || window.aBBWarnInitially ) ) { warningStr += ' &lt;' + window.aBBAngleBalance; if ( window.aBBAngleBalance !== angleBalance ) { warningStr += ' &rarr; ' + angleBalance; }		warningStr += '&gt; '; }

if ( warningStr.indexOf( ' ' ) === 0 ) warningStr = warningStr.substr( 6 ); if ( warningStr ) { warningStr = ' ' + warningStr + ' '; 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 ); }