JS.addScript reference to file?

mattgmr1
30 Sept 2020, 13:33

The "JavaScript to Quest with ASLEvent" discussion provides a cool way to get Quest and JavaScript to talk to each other. However, the "To test..." section shows a cumbersome way to parse a JavaScript function into a string "s" variable:

s = "<script>"
s = s + "function askAge() {"
s = s + "var answer = prompt('How old are you?');"
s = s + "if (answer != null && answer != '') {"
s = s + "ASLEvent('InputboxCallback', answer);"
s = s + "}}</script>"
JS.addScript(s)
JS.askAge()

Is there a way to put all that into an included JavaScript file? The following didn't work for me:

JS.addScript(GetFileURL("testscript.js"))
JS.askAge ()

The contents of testscript.js are exactly the same as "s" above except for leaving out the closing and opening <script> tags.

Thanks!


mrangel
30 Sept 2020, 15:16

That's an odd way to add a script.

I was under the impression that any *.js files included within a Quest game would be automatically loaded at the start. therefore your script would just be:

JS.askAge ()

I can't test this, because I'm limited to using the web editor, which doesn't allow you to add javascript. But if it doesn't work automatically, you could probably do either:

JS.addScript("<script src=\"" + GetFileURL("testscript.js") + "\" />")
JS.askAge ()

or

JS.addScript("<script>" + GetFileData("testscript.js") + "</script>")
JS.askAge ()

or even:

JS.eval(GetFileData("testscript.js"))
JS.askAge ()

(As I can't use external files for my javascript, I'm used to putting them through a JS compression utility that outputs a single line of code, and then just pasting that into a single JS.eval call. I'm pretty sure that's more efficient than having to wrap it i HTML and append it to the document every time)


mattgmr1
30 Sept 2020, 20:33

That did it! These two solutions both work in my desktop editor:

JS.addScript("<script>" + GetFileData("testscript.js") + "</script>")
JS.askAge ()

and:

JS.eval(GetFileData("testscript.js"))
JS.askAge ()

I believe your JS.eval solution (first running the code through minification) works, too.


mrangel
30 Sept 2020, 20:39

Yep :)

But if minifying code, make sure to escape it correctly. My usual approach is to to take the output from an online compressor, then run a pair of search/replaces on it; changing \ to \\ and " to ' (or to \" if there's already single quotes in there). Then just stick JS.eval(" on the beginning and ") on the end.

I suspect having it in a separate file is better if you can. I'd only recommend my method if you're on the web editor and so can't upload files to include.


mattgmr1
30 Sept 2020, 23:38

Just curious: why do you only use the web editor?


mrangel
01 Oct 2020, 00:18

The desktop one requires Windows.


Pertex
01 Oct 2020, 13:00

Just curious: mattgmr1, when you are using the offline editor, why don't you just import the .js file?


mattgmr1
02 Oct 2020, 10:40

I suppose I would if I knew how! I see that Squiffy has an @import command, but that doesn't seem to work in Quest.


Pertex
02 Oct 2020, 16:06

In the left tree of the editor you have "Advanced" and there is "Javascript" where you can add script files


mattgmr1
02 Oct 2020, 16:33

Fantastic! Thanks so much.