Search code examples
firefoxdomfirefox-addondomcontentloaded

Access dom after "chrome-document-global-created"


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...
}

Solution

  • 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);