reload JS: Get captcha ID using JSON for the API request

Change-Id: I0cc5293bd9dd0f0e71a3f5b3277bd8d53cb740f8
This commit is contained in:
Alex Monk 2017-08-29 13:26:42 +01:00 committed by Legoktm
parent 63800fcc4d
commit a1d0cc0e4f

View file

@ -1,30 +1,18 @@
( function ( $, mw ) {
var api = new mw.Api();
$( document ).on( 'click', '.fancycaptcha-reload', function () {
var $this = $( this ),
$root, $captchaImage;
$root = $this.closest( '.fancycaptcha-captcha-container' );
$root = $this.closest( '.fancycaptcha-captcha-container' ),
$captchaImage = $root.find( '.fancycaptcha-image' );
$this.addClass( 'fancycaptcha-reload-loading' );
$captchaImage = $root.find( '.fancycaptcha-image' );
// AJAX request to get captcha index key
api.post( {
action: 'fancycaptchareload',
format: 'xml'
}, {
dataType: 'xml'
} )
.done( function ( xmldata ) {
var imgSrc, captchaIndex;
captchaIndex = $( xmldata ).find( 'fancycaptchareload' ).attr( 'index' );
new mw.Api().post( { action: 'fancycaptchareload' } ).done( function ( data ) {
var captchaIndex = data.fancycaptchareload.index,
imgSrc;
if ( typeof captchaIndex === 'string' ) {
// replace index key with a new one for captcha image
imgSrc = $captchaImage.attr( 'src' )
.replace( /(wpCaptchaId=)\w+/, '$1' + captchaIndex );
imgSrc = $captchaImage.attr( 'src' ).replace( /(wpCaptchaId=)\w+/, '$1' + captchaIndex );
$captchaImage.attr( 'src', imgSrc );
// replace index key with a new one for hidden tag