Using api from custom component

I’m trying to access the api from a custom component with this snipped in the html body:

<script type="text/discourse-plugin" version="0.8">
  console.log("###########", api.getCurrentUser());
</script>

This snippet however does not appear in the page HTML.

What’s the correct approach here?

Btw, this is the same question posed a few years ago but not answered:

2 Likes

Hello Garrett!

If you put this code in your theme to theme component Head tab,

image

You should be able to see the output in the browser’s console (there is no HTML being displayed here):

Useful guide to learn about Theme component (also theme-component tag) :+1:

1 Like

Thank you. I had placed this code in the body section.

It’s surprising to me that the head behavior here is different. It looks like Discourse is removing these api snippets from the body section. Other script elements are retained there.

2 Likes