Skip to content

Instantly share code, notes, and snippets.

@duskohu
Created February 23, 2020 12:04
Show Gist options
  • Save duskohu/8abe3c5ecc564c4ca85c0c0a933e68fc to your computer and use it in GitHub Desktop.
Save duskohu/8abe3c5ecc564c4ca85c0c0a933e68fc to your computer and use it in GitHub Desktop.
/**
* This file is part of the ReCaptchaControl package
*
* @license MIT
* @author Petr Kessler (https://kesspess.cz)
* @link https://github.com/uestla/ReCaptchaControl
*/
;(function(window, $) {
if (!$ || typeof $.nette === 'undefined') {
console.error('nette.ajax.js library is required, please load it.');
}
// renders all recaptcha elements on page
var renderAll = function() {
$('.g-recaptcha').each(function() {
var el = $(this);
clientIDs[this.id] = grecaptcha.render(this, {
size: 'invisible',
badge: 'inline',
callback: function(token) {
el.closest('form').off('submit.recaptcha').trigger('submit');
},
}, true);
});
$(function() {
$('form').on('submit.recaptcha', function(event) {
event.preventDefault();
var form = $(this);
if (Nette.validateForm(this, true)) {
// execute only reCAPTCHAs in submitted form
$('.g-recaptcha', form).each(function() {
grecaptcha.execute(clientIDs[this.id]);
});
}
});
});
};
// set global onload callback (used in library loading below)
var callbackName = 'g_onRecaptchaLoad';
var clientIDs = {};
window[callbackName] = function() {
renderAll();
$.nette.ext('recaptcha', {
load: function() {
renderAll();
},
});
};
// load official library with explicit rendering
$('<script />', {
src: 'https://www.google.com/recaptcha/api.js'
+ '?onload=' + callbackName
+ '&render=explicit'
+ '&hl=' + ($('html').attr('lang') || 'en'),
}).insertBefore('script:first');
})(window, window.jQuery || false);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment