(function() { const fasoCaptcha = function() { this.containerId = 'faso-captcha-container'; this.options = { callback: null, canSubmitMultipleTimes: false, }; }; fasoCaptcha.prototype.render = function(container, options) { this.options = options || this.options; if (typeof container == 'string') { container = document.querySelector(container); } if (!container) { container = document.createElement('div'); container.id = this.containerId; container.className = 'g-recaptcha'; container.dataset.sitekey = '6LfIkxYUAAAAAFDSDheiBF7Yqyj4aflfAC769F3L'; container.style = 'margin: 10px 0;'; let script = document.createElement('script'); script.src = 'https://www.google.com/recaptcha/api.js?render=explicit'; let scriptEl = document.getElementById('faso-captcha'); scriptEl.insertAdjacentElement('afterend', container); scriptEl.insertAdjacentElement('afterend', script); } let form = findParent(container, 'form'); form.addEventListener('submit', (e) => onSubmit(e, container, this.options)); } function findParent(element, selector) { return element.parentElement.matches(selector) ? element.parentElement : findParent(element.parentElement, selector); } function onSubmit(e, container, options) { e.preventDefault(); let btn = e.target.querySelector('[type=submit]'); btn.disabled = true; btn.value = 'Verifying...'; let widgetId = grecaptcha.render(container, { callback: () => successCallback(e.target, widgetId, btn, options), 'error-callback': () => errorCallback(widgetId, btn), }); } function successCallback(form, widgetId, btn, options) { btn.value = 'Sending...'; if (options.callback) { options.callback(); } else { HTMLFormElement.prototype.submit.call(form); } if (options.canSubmitMultipleTimes) { resetWidget(widgetId); btn.disabled = false; btn.value = 'Submit'; } } function errorCallback(widgetId, btn) { btn.disabled = false; btn.value = 'Submit'; resetWidget(widgetId); alert('Something went wrong, please try again.'); } function resetWidget(widgetId) { grecaptcha.reset(widgetId); } window.fasoCaptcha = new fasoCaptcha(); window.fasoCaptcha.render('#faso-captcha-container'); })();