Search code examples
dojokeyboardmodal-dialogaccessibilityrecaptcha

How to keyboard navigate to reCAPTCHA in a modal dialog?


I need to open Google's latest reCAPTCHA widget in a popup (modal) dialog, a Dojo Dialog in my case, and I've got that working fine, but I just realized that the user cannot keyboard navigate to it.

When the reCAPTCHA widget is displayed in the main view, not a modal dialog, then of course the user can easily keyboard navigate to it.

Has anyone found a way to set focus on the reCAPTCHA widget so that the user can access it without a mouse when the reCAPTCHA is in a Dojo Dialog?

I did see that reCAPTCHA is generated within an <iframe>. Is that part of the hurdle - that keyboard navigation can't reach content within an iframe? I've even tried to call document.getElementById("recaptcha-anchor") since I saw that that's the id of the <span> that holds the "checkbox" - but that is returning null. How to reach an element within an iframe?

I have a jsfiddle example available for demonstration at

https://jsfiddle.net/gregorco/xqs8w5pm/5/

<script>
 var onloadCaptchaCallback = function() {
 console.log("jsfiddle: rendering captcha");
 globalRecaptchaWidgetId = grecaptcha.render('captchaDiv', {
     'sitekey' : '6LcgSAMTAAAAACc2C7rc6HB9ZmEX4SyB0bbAJvTG',
     'callback' : verifyCaptchaCallback,
     'tabindex' : 2
 });

 grecaptcha.reset();
}

var verifyCaptchaCallback = function(g_recaptcha_response) {
console.log("Response validated.  Not a robot.");
};
</script>

<script src='https://www.google.com/recaptcha/api.js?onload=onloadCaptchaCallback&render=explicit' async defer></script>

<div id="testDiv">
<button type="dojo/form/Button" onClick="captchaPopup.show();">Open reCAPTCHA</button>
</div>
<div data-dojo-type="dijit/Dialog" data-dojo-id="captchaPopup" title="Human Verification" style="width:350px;">
  Cannot keyboard navigate to the checkbox!
<table>
<tr>
  <td>
    <div id="captchaDiv"></div><br/>
  </td>
</tr>
</table>
</div>

Solution

  • Give this fiddle a try. Normally Dijit dialogs don't work too well with iframes in them because it doesn't know how to parse the content inside an iframe. In this case, we can use some of Dojo's functions to work around it. One notable thing to point out is that I've disabled autofocus of the Dijit Dialog so that it won't automatically focus the closeNode inside the dialog.

    After the dialog loads, tab>space will select the captcha.