feat: support recaptcha v3

This commit is contained in:
Niklas 2024-10-22 16:55:42 +02:00
parent af0b99e45e
commit ea34b989e1
2 changed files with 26 additions and 9 deletions

View File

@ -1671,6 +1671,10 @@ select {
pointer-events: none; pointer-events: none;
} }
.invisible {
visibility: hidden;
}
.static { .static {
position: static; position: static;
} }

View File

@ -62,15 +62,13 @@
<input type="text" name="honey_pot" style="display: none" class="hidden"> <input type="text" name="honey_pot" style="display: none" class="hidden">
{{ with .Get "recaptcha_site_key" }}
<div class="g-recaptcha" data-sitekey="{{ . }}" data-callback="submitForm{{ $random }}" data-size="invisible"></div>
{{ end }}
<div> <div>
<button <button
{{ if (.Get "recaptcha_site_key") }} id="formSubmit-{{ $random }}"
{{ with .Get "recaptcha_site_key" }}data-sitekey="{{ . }}"{{ end }}
data-callback="submitForm{{ $random }}"
data-action="submit"
{{ else }}
type="submit"
{{ end }}
class="inline-flex justify-center py-2 px-4 border border-transparent shadow-sm text-sm font-medium rounded-md text-white bg-indigo-600 hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500 dark:bg-indigo-500 dark:hover:bg-indigo-600 dark:focus:ring-offset-gray-800" > class="inline-flex justify-center py-2 px-4 border border-transparent shadow-sm text-sm font-medium rounded-md text-white bg-indigo-600 hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500 dark:bg-indigo-500 dark:hover:bg-indigo-600 dark:focus:ring-offset-gray-800" >
{{ .Get "submit_text" | default "Send" }} {{ .Get "submit_text" | default "Send" }}
</button> </button>
@ -78,7 +76,22 @@
</form> </form>
<script type="text/javascript"> <script type="text/javascript">
function submitForm{{ $random }} (token) { const recaptchaSiteKey = '{{.Get "recaptcha_site_key" }}';
document.getElementById("form-{{ $random }}").submit(); window["submitForm{{ $random }}"] = (token) => {
const form = document.getElementById("form-{{ $random }}");
console.log("form submitted");
form.submit();
} }
document.addEventListener("DOMContentLoaded", () => {
const form = document.getElementById("form-{{ $random }}");
const submit = document.getElementById("formSubmit-{{ $random }}");
submit.onclick = (e) => {
e.preventDefault();
if (form.reportValidity()) {
if (recaptchaSiteKey) grecaptcha.execute();
else window["submitForm{{ $random }}"]();
}
};
});
</script> </script>