1

I need to read response body of all js requests in a chrome tab in my extension. I am attaching debugger to tab in webRequest.onBeforeRequest method for the first request which is of type "main_frame" and url "http://localhost:8086/".

chrome.webRequest.onBeforeRequest.addListener(attachDebugger, { urls: ['*://*/*'] }, ["blocking"]);

Here is the code

    function attachDebugger(details){
     if(!isDebuggerAttached){
        tabId = details.tabId
        isDebuggerAttached = true
        chrome.debugger.attach({tabId:tabId}, "1.2")
        chrome.debugger.sendCommand({ tabId:tabId }, "Network.enable");
        chrome.debugger.onEvent.addListener(onEvent);
      }
    }

function onEvent(debuggeeId, message, params) {
if (message == "Network.requestWillBeSent" && params.type == "script") {
    console.log("requestWillBeSent =" + JSON.stringify(params))
  }
  if (message == "Network.responseReceived" && params.response && && params.type == "script"){

  chrome.debugger.sendCommand({
                  tabId: tabId
              }, "Network.getResponseBody", {
                  "requestId": params.requestId
              }, function(response) {
                      // you get the response body here!

              });
  }
}

I am getting response body for js requests whose initiator is Script but not Parser. For Example, I get response body only of login.js(Initiator: Script). enter image description here

Please Note for the first load I don't get call back for onEvent for message "Network.requestWillBeSent" for vendor.js and app.js(Initiator: Parser)

However If I reload the page, I get response body for all requests and I get callbacks for message "Network.requestWillBeSent" for vendor.js and app.js

It looks like debugger is attached after requests have been made for app.js and vendor.js which explains why everything works on reload.

If I try to attach debugger to any empty tab(before loading any website), I am getting this error “Unchecked runtime.lastError while running debugger.attach: Cannot access a chrome:// URL

When exactly should the debugger be attached?

  • Can you check this [SO post](https://stackoverflow.com/questions/28431505/unchecked-runtime-lasterror-when-using-chrome-api) if this is helpful regarding to your error? – MαπμQμαπkγVπ.0 Feb 15 '18 at 10:51

0 Answers0