« Welcome diggers and delicious people | Main | Why you need a good burr coffee grinder for your espresso shots »

Firefox 1.5 and Greasemonkey 0.6.4: where is my DOMParser

December 02, 2005
Keywords:

The good news is that Firefox 1.5 is out and for it there is also the newest Greasemonkey 0.6.4.

The bad news is that this combination breaks a lot of existing user scripts. There is a WiKi up that gives a helping hand at upgrading scripts but it is not complete (covers mainly XPCNativeWrappers, you know the unsafeWindow thingy).

I was quite surprised that the IMDB ratings for MyVue.com stopped working. It was specifically the DOMParser that stops working with the cryptic error message 'DOMParser is not a constructor'.

The instinctive thing would be to use responseXML instead of responseText but only until you remind your self that the GM_xmlhttpRequest implementation returns the same string for both functions (instead of the correct DOM object for responseXML).

I was just about to dive into manual parsing of the XML response with regexp and indexOf goodness but was literally saved by boogs from the Greaseblog.

He pointed me to the little known "new XML()" of Mozilla E4X. It can parse XML text strings and provide DOM like interface so it is good enough for the job.

There are some oddities of course. When first using it you will get a xml is a reserved identifier error. Don't ask why but the xml declaration has to be stripped out from the top of your xml document. Achieve this with responseText.replace(/<\?xml version="1.0"\?>/,'').

To get to a node use the following syntax: xmlDoc..foo[0] (for the first instance of foo tag). I found more information on the EX4 syntax at the E4X Expression Tester.

Now get busy fixing those old Greasemonkey scripts!

Update
Turns out that you can use XPCNativeWrapper to instantiate a new DOMParser (and other missing objects like XMLSerializer, HTMLBodyElement, ...) like so:

var dp = new XPCNativeWrapper(window, "DOMParser").DOMParser.

I am not sure about the security implications of doing this.



Trackback Pings

TrackBack URL for this entry:
http://blog.davidkaspar.com/cgi-bin/fsdg39fmcnswgv.cgi/185

Comments

  1. Johan Sundström Says:

    no gravatar

    Interestingly, I manage quite fine to use DOMParser without jumping through all those hoops in the Mark my links script I whipped together the last few days -- at least running Mozilla 1.5.0.1. Reports has it it doesn't work with 1.5.0, though, so your method may have slightly better portability.

    David says:

    Maybe Mozilla 1.5.0.1 again enabled the DOMParser?

    It was specifically Firefox 1.5 for which many Greasemonkey scripts broke because DOMParser had been disabled.

    I found the EX4 syntax very elegant but will try not use it because it is less portable.

    Will check out your scripts later today.

  2. Tim McCormack Says:

    no gravatar

    So, any new information about the XPCNativeWrapper approach, and the security implications thereof?

Post a Comment

 *

 *

 
Remember Me?

* (you may use HTML tags for style)