41

I'd like to read (not modify) the response body for all requests that match some pattern in a Chrome extension. I'm currently using chrome.devtools.network.onRequestFinished, which gives you a Request object with a getContent() method. This works just fine, but of course requires the devtools to be open for the extension to work. Ideally the extension would be a popup, but chrome.webRequest.onCompleted doesn't seem to give access to the response body. There is a feature request to allow the webRequest API to edit response bodies - but can webRequest even read them? If not, is there any other way to read response bodies outside of devtools extensions?

Sam Hanley
  • 4,707
  • 7
  • 35
  • 63
Josh
  • 1,277
  • 2
  • 14
  • 16

3 Answers3

10

The feature request you linked to implies that there is no support for reading either:

Unfortunately, this request is not trivial. (...) Regarding reading the Response Body: This is challenging from a performance perspective. (...) So overall, this is just not easy to achieve...

So, no, there doesn't seem to be a way for an extension to access network response bodies, except for devtools.

rbp
  • 43,594
  • 3
  • 38
  • 31
-2

Here is what I did

  1. I used the chrome.webRequest & requestBody to get the post requests body
  2. I used a decoder the parse the body into a string

Here is an example

chrome.webRequest.onBeforeRequest.addListener(
    function(details) {
        if(details.method == "POST")
        // Use this to decode the body of your post
            var postedString = decodeURIComponent(String.fromCharCode.apply(null,
                                      new Uint8Array(details.requestBody.raw[0].bytes)));
           console.log(postedString)

    },
    {urls: ["<all_urls>"]},
    ["blocking", "requestBody"]
);
Taher Elsheikh
  • 165
  • 1
  • 4
-5

If you have the this pattern of requests you can run something like that in your background.html file:

    var xhr = new XMLHttpRequest();
    xhr.open("GET", "http://example.com/" + yourStringForPattern, true);
    xhr.onreadystatechange = function() {
    if (xhr.readyState == 4) {
      var body = xhr.responseText;
      // call some function to do something with the html body

    }
   }
   xhr.send();
Community
  • 1
  • 1
Ido Green
  • 2,795
  • 1
  • 17
  • 26
  • 1
    This just shows how to make a request. The OP is asking how to monitor the response bodies of any requests the browser makes. AFAIK chrome.devtools.network.onRequestFinished is still the only way to get bodies and requires the debugger to be open. – rhashimoto Mar 03 '13 at 22:59