diff --git a/js/recaptcha.js b/js/recaptcha.js new file mode 100644 index 0000000..f1c3363 --- /dev/null +++ b/js/recaptcha.js @@ -0,0 +1,24 @@ +(function($) { + + "use strict"; + + /** + * Makes sure that captchas can continue working after AJAX calls. + */ + Drupal.behaviors.recaptchaReload = { + attach: function(context, settings) { + if (typeof(settings.recaptcha) != 'undefined' && typeof(grecaptcha) != 'undefined') { + if (document.readyState == 'complete') { + var el = $('.' + settings.recaptcha.class, context); + if (el.size() > 0 && el.children().size() == 0) { + grecaptcha.render(el.get(0), { + sitekey: el.data('sitekey'), + theme: el.data('theme') + }); + } + }; + } + } + }; + +})(jQuery); diff --git a/recaptcha.module b/recaptcha.module index e0e31e6..478839f 100644 --- a/recaptcha.module +++ b/recaptcha.module @@ -119,9 +119,21 @@ function recaptcha_captcha($op, $captcha_type = '') { '#suffix' => $noscript, ); + // Adds the recaptcha script. + $captcha['form']['recaptcha_widget']['#attached'] = array( + 'js' => array( + 'https://www.google.com/recaptcha/api.js' => array('external' => TRUE), + drupal_get_path('module', 'recaptcha') . '/js/recaptcha.js' => array(), + array( + 'data' => array('recaptcha' => array('class' => 'g-recaptcha')), + 'type' => 'setting', + ) + ), + ); + // @todo: #1664602: D7 does not yet support "async" in drupal_add_js(). // drupal_add_js(url('https://www.google.com/recaptcha/api.js', array('query' => array('hl' => $language->language), 'absolute' => TRUE)), array('defer' => TRUE, 'async' => TRUE, 'type' => 'external')); - $data = array( + /*$data = array( '#tag' => 'script', '#value' => '', '#attributes' => array( @@ -130,7 +142,7 @@ function recaptcha_captcha($op, $captcha_type = '') { 'defer' => 'defer', ), ); - drupal_add_html_head($data, 'recaptcha_api'); + drupal_add_html_head($data, 'recaptcha_api');*/ } else { // Fallback to Math captcha as reCAPTCHA is not configured.