javascriptgoogle-chromerequesttampermonkey

How to track outgoing requests and their response of a website with a Tampermonkey script?


I want to develop a Tampermonkey script that acts like Wireshark (sniffing outgoing requests and their responses) but in the current tab of Chrome (or another browser).

First, is that possible? If so, do you know anything that I can use as a starting point?

If it's not, are there other solutions to achieve this (more low-level)?


Solution

  • Yes, you certainly can, but you do not need TamperMonkey. Most browsers that allow you to develop extensions for them allow you to observe and analyze traffic.

    In the case of chrome, you need to use webRequest: https://developer.chrome.com/docs/extensions/reference/api/webRequest

    The above webpage provides instructions on permissions required, and how to use the API.

    For example:

    To register an event listener for a web request, you use a variation on the usual addListener() function. In addition to specifying a callback function, you have to specify a filter argument and you may specify an optional extra info argument.

    The three arguments to the web request API's addListener() have the following definitions:

    var callback = function(details) {...};
          var filter = {...};
          var opt_extraInfoSpec = [...];
    chrome.webRequest.onBeforeRequest.addListener(
            callback, filter, opt_extraInfoSpec);