User:XabqEfdg/trrefs.js

// below code is copied and modified from MediaWiki:Gadget-citations.js var $spinnerspan; var cbButton;

// Add a reference translation button next to the "Show changes" button function addTrRefButton { if ( document.getElementById( 'wpDiff' ) ) { var diff = document.getElementById( 'wpDiffWidget' ); cbButton = new OO.ui.ButtonWidget;

cbButton.setElementId( 'wpTrans' ) .setLabel( 'Translate refs' ) .setFlags( 'secondary' );

cbButton.on( 'click', function {			$.ajax( { timeout: 600000, // thousandths of a second, so 10 minutes type: 'POST', url: 'http://localhost:19050', data: { text: $( '#wpTextbox1' ).textSelection( 'getContents' ), },				beforeSend: function { $( cbButton.$element ).replaceWith( $spinnerspan ); },				success: function( data ) { expandedText = data.expandedtext; $( '#loader' ).replaceWith( cbButton.$element ); if ( expandedText ) { // Populate text box with received expanded text $( '#wpTextbox1' ).textSelection( 'setContents', expandedText ); // Populate edit summary textbox with received edit summary $( '#wpSummary' ).textSelection( 'setContents', editSummary ); // Click 'Show preview' button $( '#wpDiff' ).click; } else { $( '#loader' ).replaceWith( cbButton.$element ); alert( 'Error: Replacement text empty' ); }				},				error: function( data ) { $( '#loader' ).replaceWith( cbButton.$element ); alert( 'Error: Translation request failed' ); }			} );		} );		cbButton.$element.insertBefore( $(diff.nextSibling) ); } }

// Add a button to allow translating references in the text in the edit textbox mw.hook( 'wikipage.editform' ).add( function {	if( !$spinnerspan ) {		$spinnerspan = $( ' ');	}	if ( document.images ) {		// Loading gif when Translation request is processing		var spinner = new Image;		spinner.src = "//upload.wikimedia.org/wikipedia/commons/4/42/Loading.gif";		$spinnerspan.append( spinner );	}	if( $('#wpTrans').length === 0 ){		mw.loader.using( [ 'oojs-ui-core', 'oojs-ui.styles.icons-content', 'jquery.textSelection' ] ).then( addTrRefButton );	} } );