0

I need to get the DOM when the Mozilla observer event "chrome-document-global-created" is fired. Therefore, I have the following code. But how can I get the DOM from there? Data is null (according to documentation), can someone please tell me which object i have to use?

Many thanks!

observe: function(subject, topic, data) {
  which object is it? document? window? cant find anything there about dom...
}
Wladimir Palant
  • 56,865
  • 12
  • 98
  • 126

1 Answers1

0

If you look at the documentation, subject is a window. You still need to use QueryInterface() to actually get the nsIDOMWindow interface but after that things get pretty obvious:

var doc = subject.QueryInterface(Components.interfaces.nsIDOMWindow).document;
dump(doc.documentElement.innerHTML);

Note that there won't be much in the document at this point - it has just been created. As Boris Zbarsky notes in the comments, you probably want to add a DOMContentLoaded event listener to wait for the document contents to become available:

var wnd = subject.QueryInterface(Components.interfaces.nsIDOMWindow);
wnd.addEventListener("DOMContentLoaded", function(event)
{
  dump(wnd.document.documentElement.innerHTML);
}, false);
Wladimir Palant
  • 56,865
  • 12
  • 98
  • 126
  • Thanks, Wladimier! But one further question: doc.documentElement.innerHTML just returns the "top-level" nodes (, ). So i tried to get the content of the body via: doc.body.innerHTML but this seems to be empty. This is due to the event, meaning i cannot get the page's content via the "chrome-document-global-created"? – Fabian Frank Oct 09 '12 at 14:34
  • The global-created notification runs as soon as the Window object is created, before the document has been parsed. If you want the full DOM, you should add a DOMContentLoading event handler at that point, I would think. – Boris Zbarsky Oct 09 '12 at 19:37
  • @BorisZbarsky: Thank you, forgot to note that - I adjusted the answer. – Wladimir Palant Oct 10 '12 at 06:59
  • Ah okay. But i've noticed, that some extension (e.g. adblock plus) manipulate the dom before the DOMContentLoaded-Event was fired. How is this possible then? Nevertheless, the DOMSubstreeModified event gets fired before the DOMContentLoaded... – Fabian Frank Oct 10 '12 at 10:31
  • @FabianFrank: Adblock Plus doesn't manipulate the DOM. It prevents some loads via `nsIContentPolicy` and it installs a global stylesheet via `nsIStylesheetService`. In the end: you got an answer to the question you asked. You might want to ask a new question where you explain *what* you are trying to achieve, not *how* you are trying to achieve it. Maybe there are better solutions that you simply didn't think about. – Wladimir Palant Oct 10 '12 at 10:35