diff --git a/mollom.js b/mollom.js index 6cd918b..afa05ee 100644 --- a/mollom.js +++ b/mollom.js @@ -65,4 +65,52 @@ function getMollomCaptcha() { return false; } + +/** + * Attach click event handlers for Refresh CAPTCHA links. + */ +Drupal.behaviors.mollomRefreshCaptcha = { + attach: function (context, settings) { + // @todo Pass the local settings we get from Drupal.attachBehaviors(), or + // inline the click event handlers, or turn them into methods of this + // behavior object. + $('a.mollom-refresh-captcha', context).unbind(); + $('a.mollom-refresh-captcha', context).click(getNewMollomCaptcha); + return false; + } +}; + +/** + * Fetch a New Mollom CAPTCHA and output the image or audio into the form. + */ +function getNewMollomCaptcha() { + // Get the current requested CAPTCHA type from the clicked link. + var newCaptchaType = $(this).hasClass('mollom-audio-captcha') ? 'audio' : 'image'; + + var context = $(this).parents('form'); + + // Extract the Mollom session id and form build id from the form. + //var mollomSessionId = $('input.mollom-session-id', context).val(); + var formBuildId = $('input[name="form_build_id"]', context).val(); + + // Retrieve a CAPTCHA: + $.post(Drupal.settings.basePath + 'mollom/captcha/' + newCaptchaType + '/' + formBuildId, null, + function (data) { + if (!(data && data.content)) { + return; + } + // Inject new CAPTCHA. + $('.mollom-captcha-content', context).parent().html(data.content); + // Update session id. + $('input.mollom-session-id', context).val(data.session_id); + // Add an onclick-event handler for the new link. + Drupal.attachBehaviors(context); + // Focus on the CATPCHA input. + $('input[name="mollom[captcha]"]', context).focus(); + } + ); + return false; +} + + })(jQuery); diff --git a/mollom.module b/mollom.module index 315802d..1008ac1 100644 --- a/mollom.module +++ b/mollom.module @@ -2485,7 +2485,10 @@ function mollom_get_captcha(&$form_state) { 'query' => array('url' => $url), 'external' => TRUE, )); - $output = ''; + $output = ''; + $output .= '' . t('Refresh Captcha') . ''; + $output .= ''; + $output .= ''; $output .= ''; $output .= ''; $output .= ''; @@ -2504,6 +2507,9 @@ function mollom_get_captcha(&$form_state) { $captcha = theme('image', array('path' => $url, 'alt' => t('Type the characters you see in this picture.'), 'getsize' => FALSE)); $captcha = '' . $captcha . ''; $output = '' . $captcha . ''; + $output .= ''; + $output .= ' ' . t('Refresh') . ''; + $output .= ''; $output .= ' (' . t('verify using audio') . ')'; break; }