Firefox 1.5 and Greasemonkey 0.6.4: where is my DOMParser

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 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:[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!
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.

2 thoughts on “Firefox 1.5 and Greasemonkey 0.6.4: where is my DOMParser”

  1. 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 Reports has it it doesn’t work with 1.5.0, though, so your method may have slightly better portability.

    David says:
    Maybe Mozilla 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.

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.