User:Mr. Stradivarius/chessboardfix.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:Mr. Stradivarius/chessboardfix. |
/* This script fixes duplicate parameters in chess template invocations. If a
* chess template uses the previous standard invocation style, e.g. something
* like this:
{{Chess diagram
| tright
|
|=
8 |rd|nd|bd|qd|kd|bd|nd|rd|=
7 |pd|pd|pd|pd| |pd|pd|pd|=
6 | | | | | | | | |=
5 | | | | |pd| | | |=
4 | | | | |pl|pl| | |=
3 | | | | | | | | |=
2 |pl|pl|pl|pl| | |pl|pl|=
1 |rl|nl|bl|ql|kl|bl|nl|rl|=
a b c d e f g h
| The King's Gambit
}}
* Then it will be transformed into something like this:
{{Chess diagram
| tright
|
|rd|nd|bd|qd|kd|bd|nd|rd
|pd|pd|pd|pd| |pd|pd|pd
| | | | | | | |
| | | | |pd| | |
| | | | |pl|pl| |
| | | | | | | |
|pl|pl|pl|pl| | |pl|pl
|rl|nl|bl|ql|kl|bl|nl|rl
| The King's Gambit
}}
* To use it, highlight the chess template invocation that you want to fix in the
* edit window, and click the "Fix chessboards" link in the toolbar (below "what
* links here").
*
* To install the script, put the following code in [[Special:MyPage/skin.js]]:
importScript('User:Mr. Stradivarius/chessboardfix.js'); // Linkback: [[User:Mr. Stradivarius/chessboardfix.js]]
*/
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 *\|= *\n+/g, '\n\n' );
sel = sel.replace( /\n *(?:[a-zA-Z][a-zA-Z]?\b *)+\n+/g, '\n\n' );
sel = sel.replace( /\n *[bB]oard *\w+ *\n+/g, '\n' );
return sel;
} );
}
function TTscriptButton() {
addPortletLink('p-tb','javascript:wpChessboardFix()','Fix chessboards','t-chessboardfix');
}
if (wgNamespaceNumber != -1 && myContent) {
addOnloadHook(TTscriptButton);
}