4

Is there some way to intercept BrowserWindow http response body in main process without debugger?

Is it impossible to use WebRequest class and onCompleted method?

I can do that with debugger but for some reason I can not use this

await w.webContents.debugger.sendCommand('Network.enable')
  w.webContents.debugger.on('message', async (event, method, params) => {
    if (method === 'Network.dataReceived') {
      const responseBody = await w.webContents.debugger.sendCommand('Network.getResponseBody', {requestId: params.requestId})
    }
  })

UPD

I found only one solution expect debugger. It is not perfect but it solve my issue.

Use webPreference.preload to inject this monkey patching script.

let oldXHROpen = window.XMLHttpRequest.prototype.open;
window.XMLHttpRequest.prototype.open = function(method, url, async, user, password) {
 this.addEventListener('load', function() {
  console.log('data: ' + this.responseText);
 });

 return oldXHROpen.apply(this, arguments);
}

console.log may be replaced with ipc to send this data to main process

Max Vem
  • 153
  • 2
  • 8
  • does it work if you use `debugger`? I tried both `Fetch` and `Network`, but `w.webContents.debugger.sendCommand('Network.getResponseBody', {requestId: params.requestId}, (...args) => {console.log(...args)})` never returns anything in Electron (but works in a Chrome Extension). Chrome webRequest API cannot get the response body (that's probably the same for Electron). – teg_brightly May 14 '20 at 16:52
  • With webRequest you could probably redirect the request to a local file (at least it works in a Chrome Extension like this `return {redirectUrl: chrome.extension.getURL("modifiedFile.js")}`). – teg_brightly May 14 '20 at 16:58
  • @Sentero-esp12 it's does not solve my problem – Max Vem May 15 '20 at 19:51
  • To use debugger you need call `...debugger.attach("1.1")` before – Max Vem May 15 '20 at 19:52
  • Yes, i'm attaching the debugger, but it still doesn't work. I've created an issue here: https://github.com/electron/electron/issues/23594 And why cannot you use the debugger? Do you want first to intercept the response body and then modify it? – teg_brightly May 16 '20 at 10:29
  • 1
    Also maybe this can be helpful: https://stackoverflow.com/questions/48336167/get-request-and-response-body-from-chrome-api-oncompleted-method – teg_brightly May 16 '20 at 10:44
  • 1
    And this: https://stackoverflow.com/questions/18534771/chrome-extension-how-to-get-http-response-body – teg_brightly May 16 '20 at 10:50
  • Sorry for the late answer, can you create minimal, reproducible example with your code and share link? – Max Vem May 21 '20 at 18:29
  • Here is an example to reproduce the issue: https://github.com/Sentero-esp12/reproduce-fetch-network For both Fetch and Network domains – teg_brightly May 23 '20 at 10:03

0 Answers0