Re: XUL DOMContentLoaded - can't find anything about the propertoes of the returned event

Dave Royal<[email protected]> Tue, 26 Jan 2021 09:04:26 -0600
Newsgroups gmane.comp.mozilla.general
Message-ID <[email protected]>
R.Wieser <[email protected]> wrote:
> Hello all,
> 
> I'm trying create a XUL plugin to get the HTTP status of a page.   I've 
> found that I could use "window.addEventListener" with the "DOMContentLoaded" 
> argument.  The callback works, as I could see using "console.log( )"
> 
> The problem is that I cannot find much of anything about the "event" object 
> the callback provides. :-(
> 
> "event.type" returns "DOMContentLoaded" (the event I selected, duh.), but 
> have not been able to find more.  "event.status" and "event.readyState" 
> (found on the web somewhere) both return "undefined".
> 
> Does anyone know where to find more about that "event" objects 
> properties/methods - and/or tell me why the "event.status" is undefined ?
> 
> Maybe even better : example code showing how to extract the HTTP status code 
> from that "event" object ...
> 
> The involved (minimal) code is the below:
> - - - - - - - - - - - - - - - - - -
> window.addEventListener('DOMContentLoaded', EventDomLoaded, false);
> 
> function EventDomLoaded(event) {
>   console.log('EventDomLoaded: ' + event.type +" "+ event.status +" "+ 
> event.readyState);
> }
> - - - - - - - - - - - - - - - - - -
> 
> Firefox 52.5.0 ESR
> 
Perhaps event.target will refer to the document itself, in which case
event.target.readyState may work. Here on 76:
document.readyState > "complete"

But the status you want is not a property of the document. In the
current APIs it's a property of the response object:
<https://developer.mozilla.org/en-US/docs/Web/API/Response>
which is the result of fetching a file.

A document can be made up of many fetched files - you can see them
in the Network tab - and each resource has a response. What you want is
the status of the first GET, the one that corresponds to the URL.

How to get from the document object to the response object of the
original GET? I don't know. I suspect you can't. And anyway, you can't
do it in a background script in 52 because of that bug.

I unpacked the old live headers addon to see how it got responses. If
you look in components/nsHeaderInfo.js you'll see references to 
Components.interfaces.nsI... which are the pre-web-extension XPCOM
interfaces. I'm not at all familiar with this stuff, but I suspect 
that it uses nsIWebProgressListener to get the headers:
<https://developer.mozilla.org/en-US/docs/Mozilla/Tech/XPCOM/Reference/Interface/nsIWebProgressListener>

I wouldn't go there!
-- 
(Remove numerics from email address)