2

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

Peter
  • 21
  • 1
  • 2
  • are you trying to create an extension that "listens" to requests and logs something when it sniffs one out? if so, then read the [documentation for webRequest](https://developer.chrome.com/extensions/webRequest) - it's straight forward - there's even a link to samples on that page – Jaromanda X Aug 15 '17 at 05:59
  • I think this answer help you [how to print the data from post request on console](https://stackoverflow.com/questions/20564242/how-to-print-the-data-from-post-request-on-console) – Emre Gozel Aug 15 '17 at 06:12
  • [Where to read console messages from background.js in a Chrome extension?](https://stackoverflow.com/a/10258029) – wOxxOm Aug 15 '17 at 06:17
  • Yes I am @JaromandaX I'm trying to figure it out now. Struggling but I'm sure I'll get there in the end :) – Peter Aug 15 '17 at 06:27
  • try writing code, then when you have a specific problem with your code, ask again :p – Jaromanda X Aug 15 '17 at 06:33

1 Answers1

4

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.

Fabien
  • 4,862
  • 2
  • 19
  • 33