1

I know that I can get all HTTP Requests in DevTool, but now I want to get them in a chrome extension. Which API can do this job?

Yingchen
  • 101
  • 2
  • 12

2 Answers2

1

You would be looking at webRequest API with an event filter based on the tab ID.

Something like this (requires "webRequest" permission and "<all_urls>" host permission):

chrome.webRequest.onBeforeRequest.addListener(
  function(details) {
    // Do something with the details
  },
  {tabId: /* ... */, urls: "<all_urls>"}
);

P.S. I saw your other question regarding resources; note that you can further filter requests by type, e.g. "stylesheet"

Xan
  • 71,217
  • 14
  • 165
  • 189
  • I assume this will not catch requests which happened before the listener was added? If the OP intends to use this from a page-action button, it may not yield the desired results. – levi Mar 17 '15 at 16:41
  • @levi Technically, that's not specified in the question. But neither do DevTools (normally) log network requests until opened. You need to be ready for this. – Xan Mar 17 '15 at 16:45
0

chrome.webRequest is helpful but it doesn't let you read the response body in Chrome. I have a solution for reading the body here: https://stackoverflow.com/a/67390377/1226799

Justin Harris
  • 1,591
  • 1
  • 19
  • 29