Feature choice Relay Overrides

xKiv

Active member
Did the alert() work? If so, I suspect you're coming across another peculiarity of this method of doing things, which puts a slightly higher burden on the scripters: various browsers "clean up" the HTML they are given, and when jQuery fetches said HTML, it gets the browser-altered version. For the most part, the only difference is in quotes added around attributes that originally lacked them. Easy enough to work around with a matcher, but can cause some head-scratching. Which browser did you test in?

That would only affect things if you were modifying the html with client side javascript.
But you are only using javascript to retrieve the already relay-script-modified html from mafia (which doesn't know or care about what the browser does to its internal representation of said html).
Also, if you are modifying html with client side javascript, you should stop using matchers, and manipulate the DOM directly (using, say, jquery).
 

zarqon

Well-known member
That would all be true if your sentence "you are using javascript to retrieve the already relay-script-modified html from mafia" were true. But it's not, because the whole reason for this jQuery trickery in the first place is that we need to do things out of order. The jQuery is running before the relay script, not after.

There is no relay script modifying the HTML prior to its being relayed to and rendered in the browser, at which point client-side JavaScript -- which would be inserted by mafia, not a relay script -- gets the entire body from the DOM (which may be browser-altered by this point) and sends it to a relay script which includes the choice number, and then overwrites the entire body with the returned content. The relay script is being passed the HTML by the JavaScript, which is why authors of choice.123.ash scripts may find themselves needing to use matchers. Expecting scripters to do an ASH replace_string() to insert JavaScript which then replaces content on the page would be silly, yes?
 

xKiv

Active member
Oh right. *Two separate* relay scripts.

I fired up mafia and checked,
The html does not correspond to what you expect in the second relay script:

from print(post["page"]):
Code:
<input onclick="..." value="Pirates Control Room" type="button">

vs.

Code:
result.replace_string("Pirates Control Room\">","Pirates Control Room\"><br><small>flashy pirate enabled: "+get_property("dinseyGarbagePirate")+"</small>");


Maybe that type attribute was added later? Maybe something (browser) rearranged order of attributes? Maybe it's different in different browsers?

ETA: I dreamthink this would be best handled if we could treat the html as a DOM, and have functions for manipulating that using Xpath selectors, so we could have something like
Code:
DOM pageDom = build_dom(post["page"]);
...
// the xpath is probably wrong ...
foreach input_node in pageDom.find_nodes("//input[@value='Pirates Control Room']") {
  input_node.append_html('<br><small>flashy pirate enabled: "+get_property("dinseyGarbagePirate")+"</small>")');
}
...
return pageDom.toString();

and leave the worst work to a library (which is already used internally by mafia, iirc).
 
Last edited:

lostcalpolydude

Developer
Staff member
Code:
		String js = "\n<script>var loaded = false;\n" +
		"jQuery(function($){\n" +
		"  var which = parseInt($(\"input[name='whichchoice']\").val());\n" +
		"  if (!loaded && which && which > 0) jQuery.ajax({\n" +
		"    url: 'choice.'+which+'.ash',\n" +
		"    data: {whichchoice: which, page: $('body').html()},\n" +
		"    dataType: 'html',\n" +
		"    success: function(newcont) {\n" +
		"      if (newcont) $('body').html(newcont);\n" +
		"      loaded = true;\n" +
		"    },\n" +
		"  });\n" +
		"}); </script>";
		StringUtilities.singleStringReplace( buffer, "</head>", js + "</head>" );
That is what I had at the start of decorateChoice(). Just throwing it here because I'm removing it from my local copy for now.
 

digitrev

Member
Was this ever implemented? I'm assuming based on the topic category that it wasn't, but I'm just curious.
 

zarqon

Well-known member
For some reason, although it works for me as a relay script, it wasn't working when plugged into mafia's source.
 
Top