简体   繁体   中英

How do I access the data given in the onHttpRequest function in the Firefox Add-on SDK?

I am trying to read the response headers 'name' and 'value'. The end goal is to compare them to some pre-set name and a value to see if they match.

Here is what I have so far, it's the function that run every time I get a response header.

var observer = require("observer-service");
observer.add("http-on-examine-response", onHttpRequest);

function onHttpRequest(subject, data)
{
  console.log("request subject...." + subject);
  console.log("request data...." + data);
}

The output is as follows:

request subject....[xpconnect wrapped nsISupports] request data....null

I was hoping to know how to get the rest of the data out of the response.

Any help would be great, thanks.

The subject for http-on-examime-response implements nsIHttpChannel , among some other things. You may use .QueryInterface() or instanceof (which internally kinda uses QueryInteface , so that this works as well) to get to that interface.

const {Ci} = require("chrome");
if (subject instanceof Ci.nsIHttpChannel) {
  console.log("content-type", subject.getResponseHeader("content-type"));
  subject.visitResponseHeaders(function(header, value) {
    console.log(header, value);
  });
}

There are a couple of other questions around here going into more detail on how to use these notifications... Also, mxr can help a lot checkout out what interfaces there are, how it fits together and how one could use it (in particular the existing tests are great to see some uses for all kinds of stuff).

There is also the "nsITraceableChannel, Intercept HTTP Traffic" article going into more details, eg on how to use nsITraceableChannel to get the payload data from such a channel.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM