| 1 |
/** |
| 2 |
* Google reCAPTCHA v2 loader for Contact Forms. |
| 3 |
* |
| 4 |
* Each captcha field registers itself through the global queue |
| 5 |
* `accuaformRecaptcha2Queue` (so registration works whether the field markup |
| 6 |
* is parsed before or after this file loads). The Google api.js script is |
| 7 |
* injected only after the visitor starts interacting with a form, so no |
| 8 |
* third-party request is made on page load. |
| 9 |
* |
| 10 |
* accua_forms_show_recaptcha2 / accua_forms_reload_recaptcha2 / |
| 11 |
* accua_forms_onload_recaptcha2 and the accuaform_recaptcha2_* globals keep |
| 12 |
* their historical names and signatures: pages cached with the previous |
| 13 |
* inline loader markup call them directly. |
| 14 |
*/ |
| 15 |
|
| 16 |
var accuaform_recaptcha2_ajax_loaded = true; |
| 17 |
var accuaform_recaptcha2_initialized = false; |
| 18 |
var accuaform_recaptcha2_delayed = []; |
| 19 |
var accuaform_recaptcha2_loaded = {}; |
| 20 |
|
| 21 |
function accua_forms_show_recaptcha2(id, opts) { |
| 22 |
if (accuaform_recaptcha2_initialized) { |
| 23 |
accuaform_recaptcha2_loaded[id] = grecaptcha.render(id, opts); |
| 24 |
} else { |
| 25 |
accuaform_recaptcha2_delayed.push({id: id, opts: opts}); |
| 26 |
} |
| 27 |
} |
| 28 |
|
| 29 |
function accua_forms_reload_recaptcha2(id) { |
| 30 |
if ((typeof accuaform_recaptcha2_loaded[id]) != 'undefined') { |
| 31 |
grecaptcha.reset(accuaform_recaptcha2_loaded[id]); |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
function accua_forms_onload_recaptcha2() { |
| 36 |
accuaform_recaptcha2_initialized = true; |
| 37 |
for (var i = 0; i < accuaform_recaptcha2_delayed.length; i++) { |
| 38 |
accua_forms_show_recaptcha2(accuaform_recaptcha2_delayed[i]['id'], accuaform_recaptcha2_delayed[i]['opts']); |
| 39 |
} |
| 40 |
accuaform_recaptcha2_delayed = []; |
| 41 |
} |
| 42 |
|
| 43 |
(function ($) { |
| 44 |
var registered = {}; |
| 45 |
var listenerAttached = false; |
| 46 |
var apiRequested = false; |
| 47 |
var lang = ''; |
| 48 |
|
| 49 |
function loadApi() { |
| 50 |
if (apiRequested || accuaform_recaptcha2_initialized || window.grecaptcha || |
| 51 |
document.querySelector('script[src*="recaptcha/api.js"]')) { |
| 52 |
return; |
| 53 |
} |
| 54 |
apiRequested = true; |
| 55 |
var script = document.createElement('script'); |
| 56 |
script.src = 'https://www.recaptcha.net/recaptcha/api.js?onload=accua_forms_onload_recaptcha2&render=explicit&hl=' + encodeURIComponent(lang); |
| 57 |
script.async = true; |
| 58 |
document.head.appendChild(script); |
| 59 |
} |
| 60 |
|
| 61 |
// item = [containerId, renderOpts, language] |
| 62 |
function register(item) { |
| 63 |
var id = item[0]; |
| 64 |
if (registered[id]) { |
| 65 |
return; |
| 66 |
} |
| 67 |
registered[id] = true; |
| 68 |
if (item[2] && !lang) { |
| 69 |
lang = item[2]; |
| 70 |
} |
| 71 |
if (!listenerAttached) { |
| 72 |
listenerAttached = true; |
| 73 |
$(document).one('change', '.accuaforms-field-required, .pfbc-fieldwrap > *', loadApi); |
| 74 |
} |
| 75 |
accua_forms_show_recaptcha2(id, item[1]); |
| 76 |
} |
| 77 |
|
| 78 |
var queue = window.accuaformRecaptcha2Queue = window.accuaformRecaptcha2Queue || []; |
| 79 |
for (var i = 0; i < queue.length; i++) { |
| 80 |
register(queue[i]); |
| 81 |
} |
| 82 |
queue.push = function (item) { |
| 83 |
register(item); |
| 84 |
return queue.length; |
| 85 |
}; |
| 86 |
})(jQuery); |
| 87 |
|