Jump to content

User:Mr. Stradivarius/chessboardfix.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Mr. Stradivarius (talk | contribs) at 07:38, 27 October 2014 (letter replacement needs to be global). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
var myContent = document.getElementsByName( 'wpTextbox1' )[0];

function replaceSelection( replace ) {
	// Replace currently selected text with the string s.
	var len = myContent.value.length;
	var start = myContent.selectionStart;
	var end = myContent.selectionEnd;
	var sel = myContent.value.substring( start, end );

	if ( typeof( replace ) == 'function' ) {
		replace = replace( sel );
	}

	myContent.value = myContent.value.substring( 0, start )
		+ replace
		+ myContent.value.substring( end, len );
}

function wpChessboardFix() {
	replaceSelection( function ( sel ) {
		sel = sel.replace( /\n *\d+ *((?:\| *.. *)+)\| *= */g, '\n$1' );
		sel = sel.replace( /\n *\|= */g, '\n' );
		sel = sel.replace( /\n *(?:[a-zA-Z] *)+/g, '' );
		return sel;
	} );
}

function TTscriptButton() {
  addPortletLink('p-tb','javascript:wpChessboardFix()','Fix chessboards','t-chessboardfix');
}

if (wgNamespaceNumber != -1 && myContent) {
	addOnloadHook(TTscriptButton);
}