views:

109

answers:

3

I am developing a firefox extension and ideally would be able to get the whole darn DOM as a string.. forget any data structure. I just want what I see in "view source" in a buffer. I have been checking out javascript references and HTMLDocument etc. with no avail.

Ideally I would be able to write to this buffer as well (seems possible i.e. document.writeLn()) I wish there was a document.read()? Am I just a js noob?

A: 

How about document.body.innerHTML? If you need the head contents as well, you can also try document.getElementsByTagName('head')[0].innerHTML and append the two.

levik
`document` doesn't have `innerHTML` property (as least not for Firefox).
KennyTM
I am not sure if this works in practice elsewhere, but I just tested it in the firefox ext and comes back as null.
Tom Dignan
yes, yes - I mistyped. I meant document.body.innerHTML. I have now fixed it and added a suggestion for getting head content as well.
levik
i tried that, it didn't work in the firefox extension. if you've had success with this please let me know
Tom Dignan
@levik: Why not just use `document.documentElement.innerHTML` to grab the `<head>` and `<body>` together.
KennyTM
maybe for others purposes.. just putting it out there that i've tried that as well. I realize that was not directed at me
Tom Dignan
Please note that doing a string replace on this and then putting the HTML back will be both distracting and destructive. When you do this, you'll lose properties and event handlers that were set by script.
Matti Virkkunen
What if the search/replace operation needs to look for the words `span` or `head`? To protect against damage like that, you'd have to parse the HTML anyway. So traverse the DOM instead.
Daniel Earwicker
+1  A: 

Maybe you want to traverse all the nodes of the DOM? You can do this with document.childNodes, working recursively with each node.

Kinopiko
this could be a good ideaI will try it now
Tom Dignan
this worked, thanks a billion man.
Tom Dignan
A: 

Not sure if you can load jQuery from a FF extension but -

This came up just the other day as a jQuery question. You can use the jQuery selector $('*').html() to get all the html without the 'html' tags.

James Westgate