// [[:en:User:Citation bot]], but not slow=1
// Оригинал взят из [[:en:User:AManWithNoPlan/common.js]]
var $spinnerspan;
var cbButton;
function addCBetaToToolbox() {
if ( mw.config.get( 'wgCanonicalNamespace' ) !== 'Special' ) {
var pTb = document.getElementById("p-tb");
if ( !pTb ) {
return;
}
var escPageName = encodeURIComponent( mw.config.get( 'wgPageName' ));
mw.util.addPortletLink("p-tb", "//citations.toolforge.org/process_page.php?pcre=1&edit=toolbar&wiki_base=ru&page="+escPageName, 'Раскрыть источники [медленно]', 't-citationbot-slow', "Expand citations and fix formatting errors");
}
}
function addCitationBetaButton() {
if ( document.getElementById( 'wpDiff' ) ) {
var diff = document.getElementById( 'wpDiffWidget' );
cbButton = new OO.ui.ButtonWidget();
cbButton.setElementId( 'wpCitations' )
.setIcon( 'articleCheck' )
.setLabel( 'Источники [быстро]' )
.setFlags( 'secondary' );
cbButton.on( 'click', function () {
$.ajax( {
timeout: 240000, // thousandths of a second, so 4 minutes, which is basically infinite
type: 'POST',
url: 'https://citations.toolforge.org/gadgetapi.php?pcre=1',
data: {
text: $( '#wpTextbox1' ).textSelection( 'getContents' ),
summary: $( '#wpSummary' ).textSelection( 'getContents' )
},
beforeSend: function() {
$( cbButton.$element ).replaceWith( $spinnerspan );
},
success: function( data ) {
expandedText = data.expandedtext;
editSummary = data.editsummary;
$( '#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: function( data ) {
$( '#loader' ).replaceWith( cbButton.$element );
alert( JSON.stringify(data) );
}
} );
} );
cbButton.$element.insertBefore( $(diff.nextSibling) );
}
}
// Add a button to allow expanding citations 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 Citations request is processing
var spinner = new Image();
spinner.src = "//upload.wikimedia.org/wikipedia/commons/4/42/Loading.gif";
$spinnerspan.append( spinner );
}
if( $('#wpCitations').length === 0 ){
mw.loader.using( [ 'oojs-ui-core', 'oojs-ui.styles.icons-content', 'jquery.textSelection' ] ).then( addCitationBetaButton );
}
} );
// Add a toolbox button that runs Citation Bot on the most recent saved revision of the page
if ( $.inArray( mw.config.get( 'skin' ), ['monobook', 'modern', 'vector', 'timeless', 'vector-2022'] ) !== -1 ) {
$( addCBetaToToolbox );
}