Skip to content

Commit 0039633

Browse files
load turnstile when embedded
1 parent 1fc3a54 commit 0039633

2 files changed

Lines changed: 16 additions & 11 deletions

File tree

app/views/components/forms/_custom.html.erb

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@
109109
<% end %>
110110
<% if form.enable_turnstile? %>
111111
<div class="margin-top-2">
112-
<div class="cf-turnstile" data-sitekey="<%= ENV.fetch("TURNSTILE_SITE_KEY", nil) %>" data-callback="onTurnstileSuccess"></div>
112+
<div class="cf-turnstile" data-sitekey="<%= ENV.fetch("TURNSTILE_SITE_KEY", nil) %>"></div>
113113
<%= hidden_field_tag "cf-turnstile-response", nil %>
114114
</div>
115115
<% end %>
@@ -131,13 +131,3 @@
131131
autocomplete="off">
132132
</div>
133133
</form>
134-
135-
<% if form.enable_turnstile? %>
136-
<script>
137-
function onTurnstileSuccess(token) {
138-
document.querySelector("input[name='cf-turnstile-response']").value = token;
139-
}
140-
</script>
141-
142-
<script src="https://challenges.cloudflare.com/turnstile/v0/api.js" async defer></script>
143-
<% end %>

app/views/components/widget/_fba.js.erb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ function FBAform(d, N) {
3838
if (this.options.formSpecificScript) {
3939
this.options.formSpecificScript();
4040
}
41+
<% if form.enable_turnstile? %>
42+
this.loadTurnstile()
43+
<% end %>
4144
d.dispatchEvent(new CustomEvent('onTouchpointsFormLoaded', {
4245
detail: {
4346
formComponent: this
@@ -642,6 +645,18 @@ function FBAform(d, N) {
642645
modalId: function() {
643646
return `fba-modal-${this.options.formId}`;
644647
},
648+
<% if form.enable_turnstile? %>
649+
loadTurnstile: function() {
650+
let script = document.createElement("script");
651+
script.src = "https://challenges.cloudflare.com/turnstile/v0/api.js";
652+
script.async = true;
653+
script.defer = true;
654+
script.onload = function() {
655+
document.querySelector("input[name='cf-turnstile-response']").value = token;
656+
};
657+
document.head.appendChild(script);
658+
},
659+
<% end %>
645660
};
646661
};
647662

0 commit comments

Comments
 (0)