From c97cbc252e5ac3b63b2e03bf09b277af86a3c814 Mon Sep 17 00:00:00 2001 From: ccd0 Date: Tue, 9 Dec 2014 11:10:58 -0800 Subject: [PATCH] Fix race condition between our setup and Google's. --- src/Posting/QR.captcha.coffee | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/src/Posting/QR.captcha.coffee b/src/Posting/QR.captcha.coffee index a99fe7bb8..8a62d8b09 100644 --- a/src/Posting/QR.captcha.coffee +++ b/src/Posting/QR.captcha.coffee @@ -60,14 +60,25 @@ QR.captcha = $.globalEval ''' (function() { - var container = document.querySelector("#qr .captcha-container"); - container.dataset.widgetID = window.grecaptcha.render(container, { - sitekey: '<%= meta.recaptchaKey %>', - theme: document.documentElement.classList.contains('tomorrow') ? 'dark' : 'light', - callback: function(response) { - window.dispatchEvent(new CustomEvent("captcha:success", {detail: response})); + function render() { + var container = document.querySelector("#qr .captcha-container"); + container.dataset.widgetID = window.grecaptcha.render(container, { + sitekey: '<%= meta.recaptchaKey %>', + theme: document.documentElement.classList.contains('tomorrow') ? 'dark' : 'light', + callback: function(response) { + window.dispatchEvent(new CustomEvent("captcha:success", {detail: response})); + } + }); + } + if (window.grecaptcha) { + render(); + } else { + var cbNative = window.onRecaptchaLoaded; + window.onRecaptchaLoaded = function() { + render(); + cbNative(); } - }); + } })(); '''