diff --git a/drupal-recaptcha.js b/drupal-recaptcha.js new file mode 100644 index 0000000..2d5d090 --- /dev/null +++ b/drupal-recaptcha.js @@ -0,0 +1,26 @@ +(function ($, window, document) { + +Drupal.behaviors.recaptcha = { + attach: function (context, settings) { + $('.g-recaptcha', context).each(function() { + if (typeof grecaptcha == 'undefined') { + return; + } + if ($(this).hasClass('drupal-recaptcha-processed')) { + grecaptcha.reset(); + } + else { + grecaptcha.render(this, $(this).data()); + $(this).addClass('drupal-recaptcha-processed'); + } + + }); + } +} + +window.drupalRecaptchaOnload = function() { + $('.g-recaptcha').each(function() { + grecaptcha.render(this, $(this).data()); + }); +} +})(jQuery, window, document); diff --git a/recaptcha.module b/recaptcha.module index 7e6d8af..7d0fecf 100644 --- a/recaptcha.module +++ b/recaptcha.module @@ -118,20 +118,13 @@ function recaptcha_captcha($op, $captcha_type = '') { $captcha['form']['recaptcha_widget'] = array( '#markup' => '', '#suffix' => $noscript, - ); - - // @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( - '#tag' => 'script', - '#value' => '', - '#attributes' => array( - 'src' => url('https://www.google.com/recaptcha/api.js', array('query' => array('hl' => $language->language), 'absolute' => TRUE)), - 'async' => 'async', - 'defer' => 'defer', + '#attached' => array( + 'js' => array( + drupal_get_path('module', 'recaptcha') . '/drupal-recaptcha.js', + url('https://www.google.com/recaptcha/api.js', array('query' => array('hl' => $language->language, 'render' => 'explicit', 'onload' => 'drupalRecaptchaOnload'), 'absolute' => TRUE)), + ), ), ); - drupal_add_html_head($data, 'recaptcha_api'); } else { // Fallback to Math captcha as reCAPTCHA is not configured. diff --git a/recaptcha.test b/recaptcha.test index 4afedf7..c470977 100644 --- a/recaptcha.test +++ b/recaptcha.test @@ -128,7 +128,7 @@ class ReCaptchaBasicTest extends DrupalWebTestCase { // Check if there is a reCAPTCHA on the login form. $this->drupalGet('user'); $this->assertRaw($grecaptcha, '[testReCaptchaOnLoginForm]: reCAPTCHA is shown on form.'); - $this->assertRaw('', '[testReCaptchaOnLoginForm]: reCAPTCHA is shown on form.'); + $this->assertRaw('', '[testReCaptchaOnLoginForm]: reCAPTCHA is shown on form.'); $this->assertNoRaw($grecaptcha . '