diff --git a/js/recaptcha.invisible.js b/js/recaptcha.invisible.js index da7e4ce..8093b6f 100644 --- a/js/recaptcha.invisible.js +++ b/js/recaptcha.invisible.js @@ -45,25 +45,33 @@ function recaptchaOnInvisibleSubmit() { Drupal.behaviors.invisibleRecaptcha = { attach: function (context) { if (Drupal.hasOwnProperty('Ajax')) { + var originalBeforeSubmit = Drupal.Ajax.prototype.beforeSubmit; Drupal.Ajax.prototype.beforeSubmit = function (form_values, element, options) { - if ($(this.element).is(clickedSubmit) && grecaptcha.getResponse().length === 0) { + if (this.event === 'mousedown' && $(this.element).data('recaptcha-submit') && grecaptcha.getResponse().length === 0) { + clickedSubmit = this.element; options.needsRevalidate = true; this.progress.type = 'none'; clickedSubmitAjaxEvent = this.event; } + + originalBeforeSubmit.apply(this, arguments); }; - $(document).ajaxSend(function (event, jqxhr, settings) { - if (settings.needsRevalidate) { - jqxhr.abort(); - $(clickedSubmit).prop('disabled', false); - } - }); + if (!$(document).data('invisible-recaptcha-ajax-send-processed')) { + $(document).ajaxSend(function (event, jqxhr, settings) { + if (settings.needsRevalidate) { + jqxhr.abort(); + $(clickedSubmit).prop('disabled', false); + } + }); + + $(document).data('invisible-recaptcha-ajax-send-processed', true); + } } $('form', context).each(function () { var $form = $(this); if ($form.find('.g-recaptcha[data-size="invisible"]').length) { - $form.find(':submit').on({ + $form.find(':submit').data('recaptcha-submit', true).on({ 'mousedown.recaptcha': function (e) { preventFormSubmit(this, e); }, @@ -83,10 +91,21 @@ function recaptchaOnInvisibleSubmit() { function preventFormSubmit(elem, event) { if (grecaptcha.getResponse().length === 0) { // We need validate form, to avoid prevention of html5 validation. - if ($(elem).closest('form')[0].checkValidity()) { - event.preventDefault(); - event.stopPropagation(); - clickedSubmitEvent = event.type; + var form = $(elem).closest('form')[0]; + if (form && typeof form.checkValidity === 'function') { + if (form.checkValidity()) { + event.preventDefault(); + event.stopPropagation(); + clickedSubmitEvent = event.type; + validateInvisibleCaptcha(elem); + } + else { + if (typeof form.reportValidity === 'function') { + form.reportValidity(); + } + } + } + else { validateInvisibleCaptcha(elem); } }