User:Eighty5cacao/antiBracketBot.js

function checkEditBrackets { var editbox = $( '#wpTextbox1' ); if ( !editbox || editbox.length == 0 ) { //TODO: log return; }	var editboxtext = editbox.val; if ( typeof editboxtext != 'string' ) { //TODO: log return; }

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 + ' &rarr; ' + parenBalance + ') '; }

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 + ' &rarr; ' + braceBalance + '} '; }

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 + ' &rarr; ' + sqrbkBalance + '] '; }

var angleBalance = (editboxtext.match(//g)||[]).length; if ( !initialCountDone ) { window.aBBAngleBalance = angleBalance; }	if ( angleBalance != 0 && ( ( initialCountDone && window.aBBAngleBalance != angleBalance ) || window.aBBWarnInitially ) ) { warningStr += ' &lt;' + window.aBBAngleBalance + ' &rarr; ' + angleBalance + '&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 ); }