最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How get raw response body inside a Web Extension for Firefox 55? - Stack Overflow

programmeradmin1浏览0评论

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):

  • Mozilla documentation about Web Extension
    • Intercept HTTP requests
    • webRequest
    • webRequest.onHeadersReceived
    • webRequest.onBeforeRequest
    • webRequest.onBeforeSendHeaders

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++

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):

  • Mozilla documentation about Web Extension
    • Intercept HTTP requests
    • webRequest
    • webRequest.onHeadersReceived
    • webRequest.onBeforeRequest
    • webRequest.onBeforeSendHeaders

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++

Share Improve this question asked Sep 21, 2017 at 8:17 Benoît ZuBenoît Zu 1,28712 silver badges24 bronze badges 2
  • Which transfers are you trying to get the response to? AJAX? Actual web navigation (i.e. the base HTML page and resources, e.g <script src="something">)? – Makyen Commented Sep 22, 2017 at 2: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 Commented Sep 22, 2017 at 6:26
Add a comment  | 

3 Answers 3

Reset to default 10

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

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.

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.

发布评论

评论列表(0)

  1. 暂无评论