I've been bashing away at a game and one of the features I'm trying to implement involves running some JavaScript on the passage after it has rendered. I'm working with Sugarcube 2.20.0 and Twine 2.1.3.
The function I've got takes the contents of the passage class div, messes with the text strings (but preserves the HTML tags), and then outputs back into the same div. I've got it running outside Twine in a browser but there it's activated by pressing a button on a page (so the whole not running until the page is rendered thing isn't an issue there).
The general goal for this code is to have it activated for every passage (it'll then read the relevant State.variables variable and run based on that).
What I don't know is how best to accomplish that. I've tried to read the documentation but I'm unable to progress.
It's not clear whether the code should sit in one of the special passages, in a <<script>> block somewhere (a special passage?), the Story JavaScript area, or somewhere else. Or some combination of those things?
I suppose the other thing I need is to know whether this is how I should be reading/writing the passage contents?
document.getElementsByClassName("passage")[0].innerHTML;
On a normal webpage that gives me the contents of a div but it seems that's not the way to do it in Sugarcube.
Hopefully that's enough to go on for someone to point me in the right direction. I'm learning JavaScript bit by bit and documenting like a lunatic to help myself understand but there's enormous gaps in my knowledge.