String to DOM

I was working on a Greasemonkey script yesterday, and I ran into a problem for a second time. I had data (in this case, XML, in the previous case, HTML) that I had retrieved via GM_xmlhttpRequest() (Greasemonkey's cross-site-capable and slightly more flexible implementation of the XMLHTTPRequest object). I wanted to grab some small piece of information out of the huge string, but the easiest way to do this that I could think of was with big ugly Regular Expressions. The data was goddamn XML, why couldn't I use DOM? The ideal solution would be if Greasemonkey added a responseDocument field to the data that it passes to the onload callback. Since the ideal solution is not currently available, I had to cobble something else together.

Update 9 Nov: I found a far better solution today. I can hardly believe it.

var xmlDoc;
if (typeof(DOMParser) != "undefined") {
    var parser = new DOMParser();
    xmlDoc = parser.parseFromString(x, "application/xhtml+xml");
}

Greasemonkey Scripts

I discovered the Firefox extension Greasemonkey in early March 2005. I had probably heard of it before then, but I don't know why it had taken so long for me to really look at it. It's exactly the kind of thing that appeals to me: injecting Javascript (often with DOM actions, possibly with Ajax) into any web page. Every once in a while, I come up with an idea for a one-off script. They're all available from my profile on Greasemonkeyed.com.