Utiliser des variables Discourse pour personnaliser un Typeform intégré

Anyone else coming across this later, I have an updated version of @mark78’s awesome post:

Add this code under Admin > Customize > Themes > Edit CSS/HTML > HEAD

:bangbang: If you are on enterprise, you’ll have to create an empty theme component to add this.

<script type="text/discourse-plugin" version="0.8.25">
    api.decorateCooked(($elem) => {
        const iframes = $elem[0].querySelectorAll('iframe[src^="https://form.typeform.com"]');
        const userName = api.getCurrentUser()?.username;
        
        if(!userName){ return; }
        
        for (let i = 0; i < iframes.length; i++) {
            iframes[i].setAttribute('src', `${iframes[i].getAttribute('src')}&username=${userName}`);
        }
    });
</script>
  • Add a hidden field of ‘username’ in your Typeform
  • Embed your Typeform on your post by simply sharing the link. Do not use the Typeform embed feature. You will also need to allow the Typeform URL https://forms.typeform.com as an embed in your admin settings.