Remove duplication of recaptcha code, which caused issues, and improved script tag logic to check for errors.
This commit is contained in:
@@ -30,19 +30,31 @@ module ApplicationHelper
|
||||
}
|
||||
end
|
||||
|
||||
def recaptcha_execute(action)
|
||||
id = "recaptcha_token_#{SecureRandom.hex(10)}"
|
||||
|
||||
raw %Q{
|
||||
<input name="recaptcha_token" type="hidden" id="#{id}"/>
|
||||
<script>
|
||||
grecaptcha.ready(function() {
|
||||
grecaptcha.execute('#{RECAPTCHA_SITE_KEY}', {action: '#{action}'}).then(function(token) {
|
||||
document.getElementById("#{id}").value = token;
|
||||
});
|
||||
});
|
||||
</script>
|
||||
}
|
||||
end
|
||||
def recaptcha_execute(action)
|
||||
id = "recaptcha_token_#{SecureRandom.hex(10)}"
|
||||
|
||||
raw %Q{
|
||||
<input name="recaptcha_token" type="hidden" id="#{id}"/>
|
||||
<script>
|
||||
if (typeof grecaptcha !== 'undefined') {
|
||||
grecaptcha.ready(function() {
|
||||
grecaptcha.execute('#{RECAPTCHA_SITE_KEY}', {action: '#{action}'}).then(function(token) {
|
||||
document.getElementById("#{id}").value = token;
|
||||
}).catch(function(error) {
|
||||
console.error('reCAPTCHA execution error:', error);
|
||||
});
|
||||
});
|
||||
} else {
|
||||
document.addEventListener('grecaptcha-ready', function() {
|
||||
grecaptcha.execute('#{RECAPTCHA_SITE_KEY}', {action: '#{action}'}).then(function(token) {
|
||||
document.getElementById("#{id}").value = token;
|
||||
}).catch(function(error) {
|
||||
console.error('reCAPTCHA execution error:', error);
|
||||
});
|
||||
});
|
||||
}
|
||||
</script>
|
||||
}
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user