Created
January 18, 2019 11:02
-
-
Save okj579/e8b449b03413b78c92f4cdae57a1ee54 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function recaptcha(){ | |
var $forms = $('form.recaptcha-enabled'); | |
if (!$forms.length) return; | |
var tokenUpdater = new RecaptchaTokenUpdater(cdata.RECAPTCHA_SITE_KEY); | |
$forms.each(function(){ | |
var $form = $(this), | |
$input = $('<input type="hidden" name="g-recaptcha-response">').appendTo($form); | |
tokenUpdater.onUpdate(function(token){ | |
$input.val(token); | |
}); | |
$form.one('input change', tokenUpdater.start); | |
$form.on('submit', function(e){ | |
if (tokenUpdater.hasToken()) return; | |
e.preventDefault(); | |
$form.find('[type=submit]').prop('disabled', true); | |
tokenUpdater.onUpdate(function(){ | |
e.target.submit(); | |
}); | |
tokenUpdater.start(); | |
}) | |
}); | |
function RecaptchaTokenUpdater(siteKey) { | |
$.getScript('https://www.google.com/recaptcha/api.js?render=' + siteKey); | |
var token; | |
var callbacks = [function(t){ token = t; }]; | |
this.onUpdate = function(fn) { | |
callbacks.push(fn); | |
}; | |
this.hasToken = function(){ return !!token; }; | |
this.start = function once(){ | |
if (once.started) return; | |
once.started = true; | |
(function init(){ | |
if (!window.grecaptcha) return setTimeout(init, 10); | |
grecaptcha.ready(function(){ | |
update(); | |
setInterval(update, 30 * 1000); | |
}); | |
})(); | |
}; | |
function update() { | |
grecaptcha.execute(siteKey).then(function(token){ | |
$.each(callbacks, function (cb) { cb(token); }); | |
}); | |
} | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment