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 05:28, 27 October 2014 (try replacing something in every line). 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 ) {
		var lines = sel.split( '\n' );
		var ret = [];
		for (var line in lines) {
			line = line + 'foo';
			ret = ret.push( line );
		}
		return ret.join( '\n' )
	} );
}

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

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