2019-04-03 22:54:28 +00:00
|
|
|
/* eslint-disable no-jquery/no-global-selector */
|
2018-11-12 13:21:13 +00:00
|
|
|
$( document ).on( 'click', '.fancycaptcha-reload', function () {
|
2023-09-20 08:27:51 +00:00
|
|
|
const $this = $( this ),
|
2018-11-12 13:21:13 +00:00
|
|
|
$root = $this.closest( '.fancycaptcha-captcha-container' ),
|
|
|
|
$captchaImage = $root.find( '.fancycaptcha-image' );
|
2013-01-17 02:00:09 +00:00
|
|
|
|
2018-11-12 13:21:13 +00:00
|
|
|
$this.addClass( 'fancycaptcha-reload-loading' );
|
2013-01-17 02:00:09 +00:00
|
|
|
|
2018-11-12 13:21:13 +00:00
|
|
|
// AJAX request to get captcha index key
|
|
|
|
new mw.Api().post( { action: 'fancycaptchareload' } ).done( function ( data ) {
|
2023-09-20 08:27:51 +00:00
|
|
|
const captchaIndex = data.fancycaptchareload.index;
|
|
|
|
let imgSrc;
|
2018-11-12 13:21:13 +00:00
|
|
|
if ( typeof captchaIndex === 'string' ) {
|
|
|
|
// replace index key with a new one for captcha image
|
|
|
|
imgSrc = $captchaImage.attr( 'src' ).replace( /(wpCaptchaId=)\w+/, '$1' + captchaIndex );
|
|
|
|
$captchaImage.attr( 'src', imgSrc );
|
2013-01-17 02:00:09 +00:00
|
|
|
|
2018-11-12 13:21:13 +00:00
|
|
|
// replace index key with a new one for hidden tag
|
|
|
|
$( '#mw-input-captchaId' ).val( captchaIndex );
|
2019-02-10 15:44:37 +00:00
|
|
|
$( '#mw-input-captchaWord' ).val( '' ).trigger( 'focus' );
|
2016-06-09 20:59:37 +00:00
|
|
|
|
2018-11-12 13:21:13 +00:00
|
|
|
// now do the same with a selector that works for pre-1.27 login forms
|
|
|
|
$root.find( '[name="wpCaptchaId"]' ).val( captchaIndex );
|
2019-02-10 15:44:37 +00:00
|
|
|
$root.find( '[name="wpCaptchaWord"]' ).val( '' ).trigger( 'focus' );
|
2020-03-24 00:07:00 +00:00
|
|
|
|
|
|
|
// and make it accessible for other tools, e.g. VisualEditor
|
|
|
|
$captchaImage.data( 'captchaId', captchaIndex );
|
2018-11-12 13:21:13 +00:00
|
|
|
}
|
|
|
|
} )
|
|
|
|
.always( function () {
|
|
|
|
$this.removeClass( 'fancycaptcha-reload-loading' );
|
|
|
|
} );
|
2013-01-17 02:00:09 +00:00
|
|
|
|
2018-11-12 13:21:13 +00:00
|
|
|
return false;
|
|
|
|
} );
|