Javascript in web editor

It was annoying me have to crush down any javascript I want to use. So I put this in my game's start script:

foreach (o, AllObjects()) {
  if (StartsWith(o.name, "JS_FUNCTION_")) {
    if (HasString (o, "description")) {
      jstring = Replace(o.description,Chr(12),"")
      jstring = Replace(jstring,Chr(15),"")
      jstring = Replace(jstring,Chr(60)+"br/"+Chr(62),"")
      JS.eval (jstring)
    }
  }
}

Now, rooms with names starting "JS_FUNCTION_" are treated as javascripts. The script is read from the room description, which gives you a nice big area to edit code in.


You're on fire today!


Whoops.

The <br/> elements are still in there:

image


This fixed it:

jstring = Replace(jstring,"&lt;br/&gt;","")


Weird. On mine the string contains <br/>, not &lt;br/&gt;, so that's what I removed.


It might depend if Quest has made it into CDATA behind the scenes.


Wait ... the <br/> will still be there in the debugger, because that's the room description, without the replacements applied.

And there will never be a &lt;br/&gt; in the room description unless you typed <br/> in the input field. Because if there was, it would appear as the text <br/>, not a line break, when the player looks around the room.

Unless the desktop version is doing something really, really weird.
Without adding that extra 'Replace' line, can you change the JS.eval to JS.alert and tell me if the text that appears still has any <br/> in? Because if it does, I need to look again at some of the core scripts and see what I'm missing.

From my own tests, if the script pasted into the "description" text box is something like:

sayHello = function() {
  addText("Hello<br/>And hello again");
}

The string that gets stored in the JS_FUNCTION_hello.description attribute will be:

sayHello = function() {<br/>
  addText("Hello<br/>And hello again");<br/>
}

which my existing replacements would convert back to jstring:

sayHello = function() {  addText("HelloAnd hello again");}

Which isn't actually right, because that's not generating the intended output.
It looks like there's no way to distinguish between erroneous <br/>s inserted by the editor, and the ones that are actually a part of my code.

I expected it to convert the <br/> in the middle of my addText() call to &lt;br/&gt; and it be removed by the extra line KV suggested; but it seems that's not the case.

Oh, that's useless. If I go back to that room description in the online editor, my <br/> in the code has been converted to a linebreak. So apparently, the online editor is even worse than I expected for mangling HTML. That makes this method only useful for JS that isn't outputting HTML.
I've patched it now, by replacing « to <. Less useful, though, as I still have to tweak any javascript I want to add so it uses guillemets around HTML tags


EDIT:

I'm silly.

I need to end my functions with}; not just }.

This all works with your original code:

sayHi = function(){
  addText("Hello!!!");
};
sayNo = function() {
  addText("No!");
};

I guess I dropped all the functions but one at the same time I changed your line of code.

I'm normally more scientific, only changing one thing at a time...

All apologies!


Log in to post a reply.

Support

Forums