User:XabqEfdg/trrefs.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:XabqEfdg/trrefs. |
// 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 = $( '<span id="loader"></span>');
}
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 );
}
} );