I want to get the response for all the requests that happen at my electron app from the main process.
I'm -not- using a <webview>
tag, I'm using mainWindow.loadURL()
.
This only returns the headers and some other stuff, but not the response
session.defaultSession.webRequest.onCompleted({ urls: ['*://*/*'] }, function (details, callback) {
console.log(details);
});
So to retrieve the response I've tried with this:
try {
mainWindow.webContents.debugger.attach('1.3')
} catch (err) {
console.log('Debugger attach failed: ', err)
}
mainWindow.webContents.debugger.on('detach', (event, reason) => {
console.log('Debugger detached due to: ', reason)
});
mainWindow.webContents.debugger.sendCommand('Network.enable');
session.defaultSession.webRequest.onCompleted({ urls: ['*://*/*'] }, function (details, callback) {
mainWindow.webContents.debugger.sendCommand('Network.getResponseBody', { requestId: details.id.toString() }, function (body, body64) {
console.log(body);
});
});
but it outputs
UnhandledPromiseRejectionWarning: Error: No resource with given identifier found
UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either
by throwing inside of an async function without a catch block,
or by rejecting a promise which was not handled with .catch().
(rejection id: 1)
so I tried with
try {
mainWindow.webContents.debugger.attach('1.3')
} catch (err) {
console.log('Debugger attach failed: ', err)
}
mainWindow.webContents.debugger.on('detach', (event, reason) => {
console.log('Debugger detached due to: ', reason)
});
mainWindow.webContents.debugger.sendCommand('Network.enable');
mainWindow.webContents.debugger.on('message', function(event, method, params, resourceType){
if (method === 'Network.responseReceived') {
if (params.type === 'XHR') {
mainWindow.webContents.debugger.sendCommand('Network.getResponseBody', { requestId: params.requestId }, function (body, body64) {
console.log(body);
});
}
}
});
but it outputs the same:
UnhandledPromiseRejectionWarning: Error: No resource with given identifier found
UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either
by throwing inside of an async function without a catch block,
or by rejecting a promise which was not handled with .catch().
(rejection id: 1)
Any ideas?