2018-11-23 17:22:13 +00:00
|
|
|
// Captcha "errors" usually aren't errors. We simply don't know about them ahead of time,
|
|
|
|
// so we save once, then (if required) we get an error with a captcha back and try again after
|
|
|
|
// the user solved the captcha.
|
2024-04-01 16:24:00 +00:00
|
|
|
// TODO: ConfirmEdit API is horrible, there is no reliable way to know whether it is a "question"
|
|
|
|
// or "fancy" type of captcha. They all expose differently named properties in the
|
2019-02-10 15:44:37 +00:00
|
|
|
// API for different things in the UI. At this point we only support the SimpleCaptcha and
|
|
|
|
// FancyCaptcha which we very intuitively detect by the presence of a "url" property.
|
2024-06-07 03:55:13 +00:00
|
|
|
mw.loader.using( 'ext.visualEditor.targetLoader' ).then( () => {
|
|
|
|
mw.libs.ve.targetLoader.addPlugin( () => {
|
2018-11-29 16:51:27 +00:00
|
|
|
|
2020-06-30 15:26:12 +00:00
|
|
|
ve.init.mw.CaptchaSaveErrorHandler = function () {};
|
2018-11-29 16:51:27 +00:00
|
|
|
|
2020-06-30 15:26:12 +00:00
|
|
|
OO.inheritClass( ve.init.mw.CaptchaSaveErrorHandler, ve.init.mw.SaveErrorHandler );
|
2018-11-29 16:51:27 +00:00
|
|
|
|
2020-06-30 15:26:12 +00:00
|
|
|
ve.init.mw.CaptchaSaveErrorHandler.static.name = 'confirmEditCaptchas';
|
2018-11-29 16:51:27 +00:00
|
|
|
|
2020-06-30 15:26:12 +00:00
|
|
|
ve.init.mw.CaptchaSaveErrorHandler.static.matchFunction = function ( data ) {
|
2023-09-20 08:27:51 +00:00
|
|
|
const captchaData = ve.getProp( data, 'visualeditoredit', 'edit', 'captcha' );
|
2018-11-29 16:51:27 +00:00
|
|
|
|
2020-06-30 15:26:12 +00:00
|
|
|
return !!( captchaData && (
|
|
|
|
captchaData.url ||
|
|
|
|
captchaData.type === 'simple' ||
|
|
|
|
captchaData.type === 'question'
|
|
|
|
) );
|
|
|
|
};
|
2018-11-29 16:51:27 +00:00
|
|
|
|
2020-06-30 15:26:12 +00:00
|
|
|
ve.init.mw.CaptchaSaveErrorHandler.static.process = function ( data, target ) {
|
2023-09-20 08:27:51 +00:00
|
|
|
const captchaInput = new mw.libs.confirmEdit.CaptchaInputWidget(
|
|
|
|
ve.getProp( data, 'visualeditoredit', 'edit', 'captcha' )
|
|
|
|
);
|
|
|
|
ve.targetLinksToNewWindow( captchaInput.$element[ 0 ] );
|
2018-11-23 17:22:13 +00:00
|
|
|
|
2020-06-30 15:26:12 +00:00
|
|
|
function onCaptchaLoad() {
|
|
|
|
target.saveDialog.updateSize();
|
|
|
|
captchaInput.focus();
|
|
|
|
captchaInput.scrollElementIntoView();
|
|
|
|
}
|
2018-11-23 17:22:13 +00:00
|
|
|
|
2020-06-30 15:26:12 +00:00
|
|
|
captchaInput.on( 'load', onCaptchaLoad );
|
|
|
|
// Save when pressing 'Enter' in captcha field as it is single line.
|
2024-06-07 03:55:13 +00:00
|
|
|
captchaInput.on( 'enter', () => {
|
2020-06-30 15:26:12 +00:00
|
|
|
target.saveDialog.executeAction( 'save' );
|
|
|
|
} );
|
2018-11-23 17:22:13 +00:00
|
|
|
|
2020-06-30 15:26:12 +00:00
|
|
|
// Register extra fields
|
|
|
|
target.saveFields.wpCaptchaId = function () {
|
|
|
|
return captchaInput.getCaptchaId();
|
|
|
|
};
|
|
|
|
target.saveFields.wpCaptchaWord = function () {
|
|
|
|
return captchaInput.getCaptchaWord();
|
|
|
|
};
|
2018-11-23 17:22:13 +00:00
|
|
|
|
2020-06-30 15:26:12 +00:00
|
|
|
// ProcessDialog's error system isn't great for this yet.
|
|
|
|
target.saveDialog.clearMessage( 'api-save-error' );
|
|
|
|
target.saveDialog.showMessage( 'api-save-error', captchaInput.$element, { wrap: false } );
|
|
|
|
target.saveDialog.popPending();
|
|
|
|
onCaptchaLoad();
|
2018-11-23 17:22:13 +00:00
|
|
|
|
2020-06-30 15:26:12 +00:00
|
|
|
// Emit event for tracking. TODO: This is a bad design
|
|
|
|
target.emit( 'saveErrorCaptcha' );
|
|
|
|
};
|
2018-11-29 16:51:27 +00:00
|
|
|
|
2020-06-30 15:26:12 +00:00
|
|
|
ve.init.mw.saveErrorHandlerFactory.register( ve.init.mw.CaptchaSaveErrorHandler );
|
2018-11-23 17:22:13 +00:00
|
|
|
|
2020-06-30 15:26:12 +00:00
|
|
|
} );
|
2018-11-23 17:22:13 +00:00
|
|
|
} );
|