How can I turn a string of HTML into a DOM object in a Firefox extension?

Turn a string of HTML into a DOM object

I'm downloading a web page (tag soup HTML) with XMLHttpRequest and I want to take the output and turn it into a DOM object that I can then run XPATH queries on. How do I convert from a string into DOM object?

It appears that the general solution is to create a hidden iframe and throw the contents of the string into that. There has been talk of updating DOMParser to support text/html but as of Firefox 3.0.1 you still get an NS_ERROR_NOT_IMPLEMENTED if you try.

Is there any option besides using the hidden iframe trick? And if not, what is the best way to do the iframe trick so that your code works outside the context of any currently open tabs (so that closing tabs won't screw up the code, etc)?

This is an example of why I'm looking for a solution other than the iframe hack, if I have to write all that code to have a robust solution, then I'd rather keep looking for something else.

5 Answers

Ajaxian actually had a post on inserting / retrieving html from an iframe today. You can probably use the js snippet they have posted there.

As for handling closing of a browser / tab, you can attach to the onbeforeunload (msdn) event and do whatever you need to do.

Try this:

var request = new XMLHttpRequest();

request.overrideMimeType( 'text/xml' );
request.onreadystatechange = process;
request.open ( 'GET', url );
request.send( null );

function process() {
      if ( request.readyState == 4 && request.status == 200 ) {
         var xml = request.responseXML;
     }
}

Notice the overrideMimeType and responseXML.

The readyState == 4 is 'completed'.

This does not work if the response is not valid XML to begin with. If you tell Firefox to expect XML it will be strict about what it will parse.

Try creating a div

document.createElement( 'div' );

And then set the tag soup HTML to the innerHTML of the div. The browser should process that into XML, which then you can parse.

The innerHTML property takes a string that specifies a valid combination of text and elements. When the innerHTML property is set, the given string completely replaces the existing content of the object. If the string contains HTML tags, the string is parsed and formatted as it is placed into the document.

The problem with this is that I need the entire HTML document, <head> and all which this would throw away. Also I'm trying to not use existing windows / tabs because my code runs outside the context of them and I want to be resistant to a user randomly closing a window or tab making my code get interrupted (assuming Firefox is still running).

So you want to download a webpage as an XML object using javascript, but you don't want to use a webpage? Since you have no control over what the user will do (closing tabs or windows or whatnot) you would need to do this in like a OSX Dashboard widget or some separate application. A Firefox extension would also work, unless you have to worry about the user closing the browser.

Yes, I am using a Firefox extension, but most of the iframe examples use an arbitrary browser window rather than an object in the core process to be resistant to browser/tab closing.

Is there any option besides using the hidden iframe trick?

Unfortunately, no, not now. Otherwise the microsummary code you point to would use it instead.

And if not, what is the best way to do the iframe trick so that your code works outside the context of any currently open tabs (so that closing tabs won't screw up code, etc)?

The code you quoted uses the recent browser window, so closing tabs won't affect parsing. Closing that browser window will abort your load, but you can deal with it (detect that the load is aborted and restart it in another window for example) and it doesn't happen very often.

You need a DOM window for the iframe to work properly, so there's no clean solution at the moment (if you're keen on using the mozilla parser).

Call ASP . NET function from JavaScript?

Call ASP . NET function

I'm writing a web page in ASP . NET. I have some JavaScript code, and I have a submit button with a click event. Is it possible to call a method I created in ASP with JavaScript's click event? You should be using some Ajax library like : Anthem 20 Answers Well, if you don't want to do it using Ajax or any other way and just want a normal ASP . NET postback to happen, here is how you do it (without using any other libraries): It is a little tricky though... :) i. In your code file (assuming you are using C# and .NET 2.0 or later) add the following Interface to your Page class to make it look like public partial class Default : System.Web.UI.Page, IPostBackEventHandler{} ii. This should add (using Tab-Tab) this function to your code file: public void RaisePostBackEvent(string eventArgument) { } iii. In your onclick event in JavaScript, write the following code: var pageId = '<%= Page.ClientID %>';…

Read more…

JavaScript Troubleshooting Tools in Internet Explorer

JavaScript Troubleshooting Tools in IE

I use Firebug and the Mozilla JS console heavily, but every now and then I run into an IE-only JavaScript bug, which is really hard to locate (ex: error on line 724, when the source HTML only has 200 lines). I would love to have a lightweight JS tool (a la firebug) for Internet Explorer, something I can install in seconds on a client's PC if I run into an error and then uninstall. Some Microsoft tools take some serious download and configuration time. Any ideas? Have you considered Firebug Lite? There is microsoft script debugger Use a tool which can be run as a bookmarklet: - Jash - Firebug Lite I think IE's F12 works pretty well…

Read more…

MAC addresses in JavaScript

MAC addresses - JavaScript

I know that we can get the MAC address of a user via IE (ActiveX objects). Is there a way to obtain a user's MAC address using JavaScript? 8 Answers I concur with all the previous answers that it would be a privacy/security vulnerability if you would be able to do this directly from Javascript. There are two things I can think of: Using Java (with a signed applet) Using signed Javascript, which in FF (and Mozilla in general) gets higher privileges than normal JS (but it is fairly…

Read more…