summaryrefslogtreecommitdiff
path: root/extensions/ConfirmEdit/resources/ext.confirmEdit.fancyCaptcha.js
blob: a8db721806edfd24d7be45b386b0230ab742f8fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
( function ( $, mw ) {
	var api = new mw.Api();
	$( document ).on( 'click', '.fancycaptcha-reload', function () {
		var $this = $( this ), $captchaImage;

		$this.addClass( 'fancycaptcha-reload-loading' );

		$captchaImage = $( '.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' );
			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 );

				// replace index key with a new one for hidden tag
				$( '#wpCaptchaId' ).val( captchaIndex );
				$( '#wpCaptchaWord' ).val( '' ).focus();
			}
		} )
		.always( function () {
			$this.removeClass( 'fancycaptcha-reload-loading' );
		} );

		return false;
	} );
} )( jQuery, mediaWiki );