Firefox add-on SDK: get HTTP response headers

I am new to add-in development and now I am afraid of this problem. There are some questions that are somehow related, but they have not yet helped me find a solution.

So, I am developing a Firefox add-on that reads one specific header when any web page is loaded onto any tab in the browser.

I can load observer tabs, but I don't think there is a way to read the http headers inside the following (simple) code, only the URL. Please correct me if I am wrong.

var tabs = require("sdk/tabs"); tabs.on('open', function(tab){ tab.on('ready', function(tab){ console.log(tab.url); }); }); }); 

I can also read response headers by observing http events as follows:

 var {Cc, Ci} = require("chrome"); var httpRequestObserver = { init: function() { var observerService = Cc["@mozilla.org/observer-service;1"].getService(Ci.nsIObserverService); observerService.addObserver(this, "http-on-examine-response", false); }, observe: function(subject, topic, data) { if (topic == "http-on-examine-response") { subject.QueryInterface(Ci.nsIHttpChannel); this.onExamineResponse(subject); } }, onExamineResponse: function (oHttp) { try { var header_value = oHttp.getResponseHeader("<the_header_that_i_need>"); // Works fine console.log(header_value); } catch(err) { console.log(err); } } }; 

The problem (and the main source of personal confusion) is that when I read the response headers, I don't know what the request is for. I want to somehow display the request (especially the address of the request) and the response header ("the_header_that_i_need").

+4
source share
1 answer

You are almost there, look at the sample code here for more things you can do.

  onExamineResponse: function (oHttp) { try { var header_value = oHttp.getResponseHeader("<the_header_that_i_need>"); // URI is the nsIURI of the response you're looking at // and spec gives you the full URL string var url = oHttp.URI.spec; } catch(err) { console.log(err); } } 

Also, people often need to find a related tab that responds to Search for the tab that triggered the http-on-study-response event

+3
source

Source: https://habr.com/ru/post/1481121/


All Articles