-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
641f523
commit a61927d
Showing
5 changed files
with
97 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
|
||
<script src="https://www.google.com/recaptcha/api.js?render={$siteKey}"></script> | ||
|
||
<script>var siteKey = "{$siteKey}";</script> | ||
|
||
{literal} | ||
<script type="module"> | ||
function verifyRecaptcha(form, dataElement) { | ||
const { sitekey, size } = dataElement.dataset; | ||
const id = dataElement.id; | ||
|
||
if (!sitekey) return; | ||
|
||
return new Promise((resolve, reject) => { | ||
grecaptcha.execute(sitekey, {action: 'submit'}).then((token) => { | ||
if (token) { | ||
resolve(token); | ||
} | ||
|
||
reject('Invalid Captcha'); | ||
}) | ||
}); | ||
} | ||
|
||
const allForm = document.querySelectorAll('form'); | ||
|
||
allForm.forEach((form) => { | ||
const dataElement = form.querySelector('.g-recaptcha'); | ||
|
||
if (!dataElement) return; | ||
|
||
form.addEventListener("submit", async (event) => { | ||
event.preventDefault(); | ||
event.stopPropagation(); | ||
|
||
grecaptcha.ready(() => | ||
{ | ||
const response = verifyRecaptcha(form, dataElement) | ||
|
||
response.then((token) => { | ||
const customEvent = new CustomEvent('validRecaptcha', { | ||
detail: { | ||
token, | ||
form, | ||
} | ||
}) | ||
|
||
form.dispatchEvent(customEvent); | ||
}) | ||
.catch((error) => console.log(error)) | ||
} | ||
); | ||
}) | ||
}) | ||
</script> | ||
{/literal} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,23 @@ | ||
<script src="https://www.google.com/recaptcha/api.js?hl={lang attr="code"}" async defer></script> | ||
<script src="https://www.google.com/recaptcha/api.js?hl={lang attr='code'}" async defer></script> | ||
<script> | ||
window.onload = function() { | ||
var captchaDiv = document.getElementById("recaptcha-invisible"); | ||
var captchaDiv = document.querySelector(".g-recaptcha.g-invisible"); | ||
|
||
if (captchaDiv !== null) { | ||
var form = captchaDiv.parentElement; | ||
|
||
form.addEventListener("submit", function(event) { | ||
form.addEventListener("submit", function (event) { | ||
if (!grecaptcha.getResponse()) { | ||
event.preventDefault(); //prevent form submit | ||
grecaptcha.execute(); | ||
} | ||
}); | ||
|
||
onCompleted = function() { | ||
onCompleted = function () { | ||
if (form.reportValidity() !== false) { | ||
form.submit(); | ||
} | ||
} | ||
} | ||
} | ||
</script> | ||
</script> |