| 1 |
mw.loader.using( 'ext.visualEditor.targetLoader' ).then( () => { |
| 2 |
mw.libs.ve.targetLoader.addPlugin( () => { |
| 3 |
ve.init.mw.HCaptchaSaveErrorHandler = function () {}; |
| 4 |
|
| 5 |
OO.inheritClass( ve.init.mw.HCaptchaSaveErrorHandler, ve.init.mw.SaveErrorHandler ); |
| 6 |
|
| 7 |
ve.init.mw.HCaptchaSaveErrorHandler.static.name = 'confirmEditHCaptcha'; |
| 8 |
|
| 9 |
ve.init.mw.HCaptchaSaveErrorHandler.static.getReadyPromise = function () { |
| 10 |
if ( !this.readyPromise ) { |
| 11 |
const deferred = $.Deferred(); |
| 12 |
const config = require( './config.json' ); |
| 13 |
const scriptURL = new URL( config.hCaptchaScriptURL, location.href ); |
| 14 |
const onLoadFn = 'onHcaptchaLoadCallback' + Date.now(); |
| 15 |
scriptURL.searchParams.set( 'onload', onLoadFn ); |
| 16 |
scriptURL.searchParams.set( 'render', 'explicit' ); |
| 17 |
|
| 18 |
this.readyPromise = deferred.promise(); |
| 19 |
window[ onLoadFn ] = deferred.resolve; |
| 20 |
mw.loader.load( scriptURL.toString() ); |
| 21 |
} |
| 22 |
|
| 23 |
return this.readyPromise; |
| 24 |
}; |
| 25 |
|
| 26 |
ve.init.mw.HCaptchaSaveErrorHandler.static.matchFunction = function ( data ) { |
| 27 |
const captchaData = ve.getProp( data, 'visualeditoredit', 'edit', 'captcha' ); |
| 28 |
|
| 29 |
return !!( captchaData && captchaData.type === 'hcaptcha' ); |
| 30 |
}; |
| 31 |
|
| 32 |
ve.init.mw.HCaptchaSaveErrorHandler.static.process = function ( data, target ) { |
| 33 |
const self = this, |
| 34 |
siteKey = require( './config.json' ).hCaptchaSiteKey, |
| 35 |
$container = $( '<div>' ); |
| 36 |
|
| 37 |
|
| 38 |
target.saveFields.wpCaptchaWord = function () { |
| 39 |
|
| 40 |
return $( '[name=h-captcha-response]' ).val(); |
| 41 |
}; |
| 42 |
|
| 43 |
this.getReadyPromise() |
| 44 |
.then( () => { |
| 45 |
|
| 46 |
target.saveDialog.clearMessage( 'api-save-error' ); |
| 47 |
target.saveDialog.showMessage( 'api-save-error', $container, { wrap: false } ); |
| 48 |
self.widgetId = window.hcaptcha.render( $container[ 0 ], { |
| 49 |
sitekey: siteKey, |
| 50 |
callback: function () { |
| 51 |
target.saveDialog.executeAction( 'save' ); |
| 52 |
}, |
| 53 |
'expired-callback': function () {}, |
| 54 |
'error-callback': function () {} |
| 55 |
} ); |
| 56 |
target.saveDialog.popPending(); |
| 57 |
target.saveDialog.updateSize(); |
| 58 |
|
| 59 |
target.emit( 'saveErrorCaptcha' ); |
| 60 |
} ); |
| 61 |
}; |
| 62 |
|
| 63 |
ve.init.mw.saveErrorHandlerFactory.register( ve.init.mw.HCaptchaSaveErrorHandler ); |
| 64 |
} ); |
| 65 |
} ); |