10

I try to get the raw response body inside a Web Extension using Firefox 55.0.3.

Only "solutions" I have seen for now:

  • Repeat the request (I absolutly don't want to repeat the request)
  • Using Javascript to get innerHTML attribute of HTML tags such as head and body (tell me if I'm wrong, but with a solution like that I will not always have the whole content, for example I will get nothing in case of response without HTML. So it will never be the real raw response and in some case it will simply not work.)

Also, I saw this answer for Chrome (from 2015) using the debugger, but I wasn't able to do it with Firefox. This kind of solutions are interesting, I read Mozilla documentation about devtools but I didn't find a way of using the network tab of webtools interface with Javascript inside a Web Extension.

To give you more details, my goal is to intercept the full request and response from server (header and body). This is not a problem to do it, except for the response body.

Here an example of code to get the request body: (background script)

browser.webRequest.onBeforeRequest.addListener(
    function (e) {
        console.log(e);
    },
    {urls: ["http://*/*", "https://*/*"]},
    ["requestBody"]
)

Here some documentations that I used (there is more, but these links are all official):

Here some examples of Web Extensions.

Any ideas, solutions or even explainations "why this is not possible" are welcome, thank you in advance for your time !

Cheers++

Benoît Zu
  • 1,217
  • 12
  • 22
  • Which transfers are you trying to get the response to? AJAX? Actual web navigation (i.e. the base HTML page and resources, e.g ` – Makyen Sep 22 '17 at 02:18
  • The raw headers and body of all HTTP(S) request and response. I don't care how is done the request. – Benoît Zu Sep 22 '17 at 06:26

3 Answers3

9

This is now available, as of Firefox 57: browser.webRequest.filterResponseData allows you to add a listener via browser.webRequest.onBeforeRequest which receives, and allows you to modify the response.

You can see an example in the Mozilla github webextensions-examples repo

410 gone
  • 782
  • 14
  • 25
  • 2
    Awesome ! It works well, thank you very much to share this :) – Benoît Zu Dec 14 '17 at 10:39
  • unfortunately that example doesn't work for responses that are streamed or chunked because `filter.disconnect` is called before the rest of the response is processed. more advanced examples can be found here: https://wiki.developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/webRequest/StreamFilter/ondata – seanlinsley Dec 07 '19 at 19:46
4

Firefox 57 is going to provide the API browser.webRequest.filterResponseData. This doesn't seem to be documented yet, but you can look through bug 1255894 for details.

evilpie
  • 2,718
  • 20
  • 21
3

Why is this not possible?

For the simple reason that WebRequest was ported over from Chrome extensions, where this is explicitly impossible.

Requests for such functionality (to edit, or just to read) has been around for a very long time (since 2011 and 2015 respectively); they are challenging from both the security perspective and technical perspective, however a principal agreement that read access is a good idea is there.

However, it's simply not yet implemented. Rob W has been doing some work in this direction but it's not done yet.

Perhaps Firefox has a different implementation?

A cursory glance on Mozilla bugtracker doesn't find any bugs on providing this functionality. So, it's not likely that the implementation will diverge anytime soon.

Any workarounds?

Well, only the debugger-level access can touch actual response data.

Since debugger is not implemented in the WebExtension platform, only a devtools.network-using extension can access it - and only while Dev Tools are open for the tab making said request, which is the main limitation of devtools.* APIs.

Xan
  • 74,770
  • 16
  • 179
  • 206
  • Thank you for your answer ! I hope it will move forward and I will try to use `devtools.network` in the mean time. – Benoît Zu Sep 21 '17 at 09:54