Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chrome API responseHeaders

Based on this documentation: https://developer.chrome.com/extensions/webRequest.html#event-onHeadersReceived

I tried to display the response via the console like:

console.log(info.responseHeaders);

But its returning undefined.

But this works though:

console.log("Type: " + info.type);

Please help, I really need to get the responseHeaders data.

like image 235
madziikoy Avatar asked Apr 09 '13 01:04

madziikoy


People also ask

How do I send a post request extension in Chrome?

Type the url in the main input field and choose the method to use: GET/POST/PUT/DELETE/PATCH. Click on the arrow "Send" or press Ctrl+Enter. You'll see info about the response (time, size, type) and you'll be able to see the content response in the response section.

Can I use webRequest?

To use the webRequest API for a given host, an extension must have the "webRequest" API permission and the host permission for that host. To use the "blocking" feature, the extension must also have the "webRequestBlocking" API permission.


1 Answers

You have to request the response headers like this:

chrome.webRequest.onHeadersReceived.addListener(function(details){
  console.log(details.responseHeaders);
},
{urls: ["http://*/*"]},["responseHeaders"]);

An example of use. This is one instance of how I use the webRequest api in my extension. (Only showing partial incomplete code)

I need to indirectly access some server data and I do that by making use of a 302 redirect page. I send a Head request to the desired url like this:

$.ajax({
  url: url,
  type: "HEAD"
  success: function(data,status,jqXHR){
    //If this was not a HEAD request, `data` would contain the response
    //But in my case all I need are the headers so `data` is empty
    comparePosts(jqXHR.getResponseHeader('redirUrl')); //where I handle the data
  }     
});

And then I silently kill the redirect while scraping the location header for my own uses using the webRequest api:

chrome.webRequest.onHeadersReceived.addListener(function(details){
  if(details.method == "HEAD"){
    var redirUrl;
    details.responseHeaders.forEach(function(v,i,a){
      if(v.name == "Location"){
       redirUrl = v.value;
       details.responseHeaders.splice(i,1);
      }
    });
    details.responseHeaders.push({name:"redirUrl",value:redirUrl});
    return {responseHeaders:details.responseHeaders}; //I kill the redirect
  }
},
{urls: ["http://*/*"]},["responseHeaders","blocking"]);

I actually handle the data inside the onHeadersReceived listener, but this way shows where the response data would be.

like image 58
BeardFist Avatar answered Oct 21 '22 03:10

BeardFist