我想从主进程中获取电子应用程序中发生的所有请求的响应。
此图显示我想要获得的响应位于“响应”选项卡,而不是 Chrome 开发工具上的“标头”选项卡 https://i.stack.imgur.com/AYA1G.png.
我没有使用<webview>
标签,我正在使用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);
});
因此,为了检索我尝试过的响应:
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);
});
});
但它输出
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)
所以我尝试了
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);
});
}
}
});
但它输出相同:
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)
有任何想法吗?