【发布时间】:2020-11-29 12:10:24
【问题描述】:
我想从主进程获取我的电子应用程序中发生的所有请求的响应。
我没有使用<webview> 标签,我使用的是mainWindow.loadURL()。
这仅返回标头和其他一些内容,但不返回响应
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)
有什么想法吗?
【问题讨论】:
标签: javascript node.js npm electron