[Greasemonkey] e4x

Mook mook.moz+sites.org.mozdev.lists.greasemonkey at gmail.com
Fri Feb 10 23:53:51 EST 2006


(Replies inline; snipped parts I'm not responding to)

On 2/10/06, Stéphane Payrard <stef at stefp.dyndns.org> wrote:
> How one can use e4x in a greasemonkey script?
> What are the best ressources for scripting in firefox?
> I have GreaseMonkey hacks but I have not fisnished reading it yes.
> Mark Thx for that good book.
>
> The best uptodate ressource I know for firefox is
> http://developer.mozilla.org/ but is pretty short and cryptic on e4x :
> http://developer.mozilla.org/en/docs/e4x
>
> Here what it says :
>
<snip>
>
> I don't know what they mean by partially enabled.

I think it means that, for scripts which don't request it, it's not
enabled - otherwise it'd be a backward compatibility problem since
some pages have HTML comments in their JS (which, as e4x, would mean
their whole script is a comment).  So, in a normal web page, a
<script> by default will not support e4x.  Greasemonkey seems to
though, which ever way it's inserting the scripts.

>
> Also, I don't know enough about CDATA and comments to know
> how they works on different browser.
> Apparently it allows to write inline scripts without escaping
> charactere like '<'.
>
As a greasemonkey script, you don't need to worry different browsers -
it's not going to work anywhere else anyway ;)  CDATA sections
basically say "don't parse this stuff as XML until you hit the end,
which is a literal ]]> ".  So you can use <, > etc and not worry about
XML wellformedness.  For example, <r>while(i<1);</r> is a parse error
(is that a <1> tag or something?), but <r><![CDATA[while(i<1);]]></r>
is not - it's basically moving the escaping to the start of the text
block.

The example you showed uses CDATA to make sure it doesn't try to parse
the insides for XML. And to support older browsers, it uses JS style
comments to hide the <![CDATA[ string (which would be a JS error
without e4x).  Which in my opinion is silly, since you could just not
use e4x if you don't want it...

<snip>
>
> Finally, I can't see how to apply these idioms do greasemonkey.
>
No, they don't apply, since you're not actually *in* XML until you
start one.  If you do get into one though, you can use CDATA freely to
avoid parse errors.

<link-spam>
I'm using e4x as a hack to get multiline strings in greasemonkey -
that might be useful to someone :)  After all, JS hasn't got
HEREDOC...
http://mook.wordpress.com/2005/10/30/multi-line-strings-in-javascript/
</link-spam>

--
Mook
mook.moz at gmail


More information about the Greasemonkey mailing list