Peter
Peter

Reputation: 21

How do I console.log POST requests with my chrome extension

I'm trying to console.log POST requests with my chrome extension but can't figure out how to do it, can anyone give me an example? I've looked at the chrome extension API but still can't seem to get to it

Upvotes: 2

Views: 6969

Answers (1)

Fabien
Fabien

Reputation: 4972

In Google Chrome, browser requests such as POST and GET are visible in the Network tab of the Inspector.

Screenshot from Chrome Devtools Overview:

enter image description here

If you are looking for a natural way to make a Javascript hook on browser requests (such as for logging them out), you will have more issues as there is no native way for Javascript to hook on requests at browser's scale, for security reasons.

But if you are okay to use a dedicated extension for the job, you can look at the webRequest extension for Chrome:

https://developer.chrome.com/extensions/webRequest

Use the chrome.webRequest API to observe and analyze traffic and to intercept, block, or modify requests in-flight.

Here's an example of listening for the onBeforeRequest event:

  chrome.webRequest.onBeforeRequest.addListener(callback, filter, opt_extraInfoSpec);

Beware, there are security requirements and limitations:

You must declare the "webRequest" permission in the extension manifest to use the web request API, along with host permissions for any hosts whose network requests you want to access.

Note that due to the ordered or asynchronous nature of the webpage resources loading (HTTP/1.x or HTTP/2.0) you are not be guaranteed to catch all the requests made by the browser that happened before your Javascript hooks were setup.

Eventually, you have some tricks, such as those referenced here for detecting AJAX calls through Javascript proxification mechanisms.

Another strategy would be to have the request detection deported on server and informing the client he sent a request (through Websockets/queues for example). It would only work for requests targeted on the domains you manage though, and it sounds like a bit of an expensive solution. It all depends of what your ultimate needs are.

Upvotes: 4

Related Questions