Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 43 additions & 10 deletions ReCaptchaFrontendUi/view/frontend/web/js/reCaptcha.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,34 @@ define(
*/
initialize: function () {
this._super();

this.attachFocusEvent();
this._apiLoaded = false;
},

attachFocusEvent: function () {
const self = this, $parentForm = $('#' + this.getReCaptchaId() + '-container').parents('form');
/**
* Defers reCAPTCHA API loading until the form DOM exists.
* Loads on first form field focus, with an idle-callback fallback
* for iframes and programmatic-submit scenarios.
*
* @param {jQuery} $parentForm
* @private
*/
_deferApiLoad: function ($parentForm) {
var self = this,
load = function () {
if (!self._apiLoaded) {
self._apiLoaded = true;
$parentForm.off('focus.recaptcha', 'input, select, textarea', load);
self._loadApi();
}
};

$parentForm.one('focus.recaptcha', 'input, select, textarea', load);

$parentForm.one('focus', 'input, select, textarea', function () {
self._loadApi();
});
if (window.requestIdleCallback) {
window.requestIdleCallback(load, {timeout: 3000});
} else {
setTimeout(load, 1000);
}
},

/**
Expand Down Expand Up @@ -213,10 +231,25 @@ define(
* Render reCAPTCHA
*/
renderReCaptcha: function () {
if (window.grecaptcha && window.grecaptcha.render) { // Check if reCAPTCHA is already loaded
var $wrapper = $('#' + this.getReCaptchaId() + '-wrapper'),
$parentForm = $wrapper.parents('form');

if (!this._apiLoaded) {
if (window.grecaptcha && window.grecaptcha.render) {
// API already present, skip deferred load
this._apiLoaded = true;
} else if ($parentForm.length) {
this._deferApiLoad($parentForm);
} else {
this._apiLoaded = true;
this._loadApi();
}
}

if (window.grecaptcha && window.grecaptcha.render) {
this.initCaptcha();
} else { // Wait for reCAPTCHA to be loaded
$(window).on('recaptchaapiready', function () {
} else {
$(window).one('recaptchaapiready', function () {
this.initCaptcha();
}.bind(this));
}
Expand Down