klembot / twinejs

Twine, a tool for telling interactive, nonlinear stories
https://twinery.org
GNU General Public License v3.0
2k stars 296 forks source link

A method to detect when HTML is launched from Twine #620

Open HiEv opened 4 years ago

HiEv commented 4 years ago

I try use automatic detection of when the HTML file is launched from the downloaded version of Twine in order to determine whether to use a relative or absolute path to my files. In earlier versions of Twine I could use window.hasOwnProperty("storyFormat") to detect if the game was playing within Twine, and then use an absolute path when it was. Unfortunately, since Twine v2.3.2 I've had to rely on looking at the URL (via. document.location.href) for particular strings (e.g. "/temp/" on Windows). However, this code is unreliable, since other OSes have different temp directories, so there is no "one size fits all" solution currently.

What I'd like is either something injected into the HTML or put within the URL when the story is played from Twine, which could easily be detected to reliably determine whether the game has been launched from Twine, as opposed to being opened from a published version of the HTML file.

Some injected HTML which immediately sets "window.storyFormat = $formatName;", "window.TwineLaunched = true;", or the like would be ideal, but even something like putting a special attribute inside the <tw-storydata> element would work. Adding a string to the URL which could be detected and is unlikely to be there normally could also work, but is less ideal.

Alternately, a way to launch the HTML from a chosen directory would do away with the need to detect this, so that could work as well.

Thank you. :-)

Gamewise commented 4 years ago

Hear hear! That would be really, really helpful!