How to execute JS after post content has rendered?

AI-generated summary

Executing JavaScript after post content has rendered

Sparky is trying to measure the offset of content within the main topic body using getBoundingClientRect, but it always returns zero because the content hasn’t been rendered yet. The issue is with the timing, and Sparky is looking for an event to listen for or a hook to use.

sam suggests queuing the JavaScript after the next runloop using a technique similar to the one used in Discourse’s page-tracker.js.

I found api.decorateCookedElement, and I see the note about afterAdopt: true, but that doesn’t get where I’m trying to go.

I’m trying to measure the offset of content within the main topic body with getBoundingClientRect, but it’s always zero because it’s never actually on the page yet…

My code works fine when triggered later (manually through the console, or through an event listener on resize or something, so the issue is just the timing).

This can be worked around by getting the offset of the main content wrapper (#topic), but then I have to manually subtract the avatar width in my calculation, which feels like a kludge and potentially will miss some edge cases of where the decorated elements actually end up sitting.

I also checked for plugin-outlets, but didn’t see any that appeared promising, and decorateWidget got my hopes up but also didn’t seem to apply to posts and post content generically.

Is this possible? Is there an event fired to listen for, or option or hook I’m missing?

You could possibly queue this after next runloop, like this.