最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

firefox - Get entire text of document as a string using javascript - Stack Overflow

programmeradmin6浏览0评论

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?

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?

Share Improve this question asked Apr 2, 2010 at 7:51 user132014user132014 5
  • Be more specific, what exactly are you trying to achieve with this? "Writing into the DOM" isn't nearly as trivial as it sounds (would end up recreating elements, losing event bindings, whatnot), and in most cases, it's just plain wrong. The DOM is a data structure and it should be modified as such, not first serialized and then deserialized again. – Matti Virkkunen Commented Apr 2, 2010 at 7:53
  • I want to do a search and replace on it – user132014 Commented Apr 2, 2010 at 7:54
  • What kind of content are you looking to replace? Text, tags, attributes? – Matti Virkkunen Commented Apr 2, 2010 at 7:57
  • i had to type 3 words, because of the filter – user132014 Commented Apr 2, 2010 at 7:58
  • Then you should just recursively loop through the DOM tree looking for the text nodes while replacing their content. Of course, if there's tags between the text it will not be caught. Replacing text that has tags in it is not quite trivial. – Matti Virkkunen Commented Apr 2, 2010 at 8:01
Add a ment  | 

4 Answers 4

Reset to default 10
document.body.innerHTML

This should work

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

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.

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.

发布评论

评论列表(0)

  1. 暂无评论