mediawiki-extensions-Confir.../ReCaptchaNoCaptcha/resources/ve-confirmedit-reCaptchaNoCaptcha/ve.init.mw.NoCaptchaReCaptchaSaveErrorHandler.js
James D. Forrester 9ef3132dce build: Upgrade eslint-config-wikimedia from 0.10.1 to 0.11.0
Change-Id: I1e6c500b8a79f8d01b3050dabdece36e77d17706
2019-04-03 15:54:28 -07:00

71 lines
2.2 KiB
JavaScript

mw.libs.ve.targetLoader.addPlugin( function () {
ve.init.mw.NoCaptchaReCaptchaSaveErrorHandler = function () {};
OO.inheritClass( ve.init.mw.NoCaptchaReCaptchaSaveErrorHandler, ve.init.mw.SaveErrorHandler );
ve.init.mw.NoCaptchaReCaptchaSaveErrorHandler.static.name = 'confirmEditNoCaptchaReCaptcha';
ve.init.mw.NoCaptchaReCaptchaSaveErrorHandler.static.getReadyPromise = function () {
var onLoadFn = 'onRecaptchaLoadCallback' + Date.now(),
deferred, config, scriptURL, params;
if ( !this.readyPromise ) {
deferred = $.Deferred();
config = mw.config.get( 'wgConfirmEditConfig' );
scriptURL = new mw.Uri( config.reCaptchaScriptURL );
params = { onload: onLoadFn, render: 'explicit' };
scriptURL.query = $.extend( scriptURL.query, params );
this.readyPromise = deferred.promise();
window[ onLoadFn ] = deferred.resolve;
mw.loader.load( scriptURL.toString() );
}
return this.readyPromise;
};
ve.init.mw.NoCaptchaReCaptchaSaveErrorHandler.static.matchFunction = function ( data ) {
var captchaData = ve.getProp( data, 'visualeditoredit', 'edit', 'captcha' );
return !!( captchaData && captchaData.type === 'recaptchanocaptcha' );
};
ve.init.mw.NoCaptchaReCaptchaSaveErrorHandler.static.process = function ( data, target ) {
var self = this,
config = mw.config.get( 'wgConfirmEditConfig' ),
siteKey = config.reCaptchaSiteKey,
$container = $( '<div>' );
// Register extra fields
target.saveFields.wpCaptchaWord = function () {
// eslint-disable-next-line no-jquery/no-global-selector
return $( '#g-recaptcha-response' ).val();
};
this.getReadyPromise()
.then( function () {
if ( self.widgetId ) {
window.grecaptcha.reset( self.widgetId );
} else {
target.saveDialog.showMessage( 'api-save-error', $container );
self.widgetId = window.grecaptcha.render( $container[ 0 ], {
sitekey: siteKey,
callback: function () {
target.saveDialog.executeAction( 'save' );
},
'expired-callback': function () {},
'error-callback': function () {}
} );
target.saveDialog.updateSize();
}
target.emit( 'saveErrorCaptcha' );
} );
};
ve.init.mw.saveErrorHandlerFactory.register( ve.init.mw.NoCaptchaReCaptchaSaveErrorHandler );
} );