HTMLDocument is null in bootstrap.js

30 views
Skip to first unread message

Marcello Stanisci

unread,
Jun 5, 2015, 7:58:54 AM6/5/15
to dev-w...@lists.mozilla.org
Hello,

The problem is how to check whether a document is an instance of HTMLDocument in
the code of a bootstrapped extension.

I'm trying to follow the example shown in [1] (under the paragraph "The easy way, load events"),
doing what is suggested in the example in my bootstrap.js.

The Browser Console shows something like "HTMLDocument undefined (or null)"; how to refer to
'HTMLDocument' from within bootstrap.js?

unfortunately, it seems quite hard to find sutiable information on MDN

Thank you guys
Marcello


[1] https://developer.mozilla.org/en-US/Add-ons/Overlay_Extensions/XUL_School/Intercepting_Page_Loads

--
Public PGP key at: http://stanisci.web.cs.unibo.it/mstan

Boris Zbarsky

unread,
Jun 5, 2015, 8:38:46 AM6/5/15
to dev-w...@lists.mozilla.org
On 6/5/15 7:58 AM, Marcello Stanisci wrote:
> The problem is how to check whether a document is an instance of HTMLDocument in
> the code of a bootstrapped extension.

If you're OK with not including synthetic image documents, the simplest
thing might be:

Object.prototype.toString.call(doc) == "[object HTMLDocument]"

If you want to include them, you could do that explicitly by also
testing for "[object ImageDocument]", of course.

Alternately, if you only care about documents in a window, then you
could do:

doc.defaultView && doc instanceof doc.defaultView.HTMLDocument

(or skip the doc.defaultView test if you know it has one).

It's also possible that doing this:

doc instanceof Components.interfaces.nsIDOMHTMLDocument

would work, thought I personally would prefer it if you used one of the
other things so wecan work on killing off nsIDOMHTMLDocument. ;)

-Boris
Reply all
Reply to author
Forward
0 new messages