CaptchaFox / javascript-integrations

Javascript integrations for CaptchaFox, the GDPR compliant and user-friendly bot management solution
https://captchafox.com/
7 stars 0 forks source link

Dynamically loading CaptchaFox from a Stimulus Controller #39

Open reiz opened 2 weeks ago

reiz commented 2 weeks ago

First of all, thanks for providing a really great captcha solution, hosted in Germany!!

I'm using it in a Ruby on Rails application withe Hotwire Turbo and Stimulus.JS. I would like to load CaptchaFox on demand, from a Stimulus Controller like this:

import { CaptchaFox } from "https://cdn.captchafox.com/api.js"

But that doesn't work. Always getting: Cannot use import statement outside a module. What I'm doing wrong? How can I load and render it on demand? Without putting this line into my templates:

<script src="https://cdn.captchafox.com/api.js" ></script>

Any input is appreciated.

tgrassl commented 2 weeks ago

Hello @reiz,

thank you for your message. To load it on demand using this framework you would need to load the script tag into the DOM manually, as a module import won't work here. You can hook yourself into the onload event of this script and then access the API object from the client.

This is similar to the manual integration process: https://docs.captchafox.com/getting-started#manual-integration