Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Patch, required due to missing g-recaptcha-response #126

Closed
idoenk opened this issue Mar 24, 2020 · 0 comments
Closed

Patch, required due to missing g-recaptcha-response #126

idoenk opened this issue Mar 24, 2020 · 0 comments

Comments

@idoenk
Copy link

idoenk commented Mar 24, 2020

This patch is work-around in form using ajax.

Patch applied on: InvisibleReCaptcha.php::renderFooterJS#L131
    public function renderFooterJS($lang = null)
    {
        $html = '<script src="' . $this->getCaptchaJs($lang) . '" async defer></script>' . PHP_EOL;
        $html .= '<script>var _submitForm,_captchaForm,_captchaSubmit,_execute=true;</script>';
        $html .= "<script>window.addEventListener('load', _loadCaptcha);" . PHP_EOL;
        $html .= "function _loadCaptcha(){";
        if ($this->getOption('hideBadge', false)) {
            $html .= "document.querySelector('.grecaptcha-badge').style = 'display:none;!important'" . PHP_EOL;
        }
        $html .= '_captchaForm=document.querySelector("#_g-recaptcha").closest("form");';
        $html .= "_captchaSubmit=_captchaForm.querySelector('[type=submit]');";
        $html .= '_submitForm=function(t){if(typeof _submitEvent==="function"){_submitEvent(t);';
        $html .= 'grecaptcha.reset();}else{_captchaForm.submit();}};';
        $html .= "_captchaForm.addEventListener('submit',";
        $html .= "function(e){e.preventDefault();if(typeof _beforeSubmit==='function'){";
        $html .= "_execute=_beforeSubmit(e);}if(_execute){";
        $html .= "grecaptcha.execute().then(function(token){_submitForm(token)});}});";
        if ($this->getOption('debug', false)) {
            $html .= $this->renderDebug();
        }
        $html .= "}</script>" . PHP_EOL;
        return $html;
    }

Notes:
grecaptcha.execute().then(function(token){_submitForm(token)});

By using promise, token value of g-recaptcha-response will be returned after callback then, then execute defined _submitForm above with param t which will be transfered to _submitEvent.

In your page containing form using recaptcha
/**
 * Use this to check mandatory field in form
 * @return mixed
 * false, submission should halted
 * true, continue submission
 */
_beforeSubmit = function(){};



/**
 * Main event submission
 */
_submitEvent = function(token){
  // Flag to avoid Bubble
  if (window['_submitEvent_hited']){
    console.log('Hit, exiting..');
    return !1;
  }
  window['_submitEvent_hited'] = true;

  var postdata = [];
  // Collect post data
  $form.find('[name]').each(function(){
    var $el = $(this),
        name = $el.attr('name')
    ;
    postdata[name] = $el.val();
  });

  if (token)
    postdata['g-recaptcha-response'] = token;

 // Do your xhr here...
 // $.ajax({.....})
 /*
  $.ajax({
    method: "POST",
    url: $form.attr('action'),
    data: postdata,
    dataType: "json",
    cache: !1
  })
  .done(function(data){ 
    console.log('xhr: done');
  })
  .fail(function(r){
    console.log('xhr: fail');
  })
  .always(function(r){
    // Dont forget to reset this state
    window['_submitEvent_hited'] = null;
  });
 */
};
@idoenk idoenk closed this as completed May 16, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant