用electron得到請(qǐng)求頁(yè)面時(shí)XHR的ResponseBody
利用electron訪問(wèn)一些頁(yè)面時(shí)打却,想要獲取到異步加載的內(nèi)容须喂。
如果利用webContents監(jiān)聽事件did-get-response-details
滑黔,只能得到以下信息:
-
event
Event -
status
Boolean -
newURL
String -
originalURL
String -
httpResponseCode
Integer -
requestMethod
String -
referrer
String -
headers
Object -
resourceType
String
并沒有想要的response body。
經(jīng)過(guò)請(qǐng)教與搜索之后萧朝,利用Debugger結(jié)合Chrome Debugging Protocol的Network Domain可以拿到response body。
Network domain allows tracking network activities of the page. It exposes information about http, file, data and other requests and responses, their headers, bodies, timing, etc.
win.webContents.debugger.on('message', (event, method, params) => {
if (method === 'Network.responseReceived') {
if(params.response.url.indexOf('xxxxx') > 0) {
console.log('Event: responseReceived ' + params.requestId + '-' + params.response.url)
win.webContents.debugger.sendCommand('Network.getResponseBody', {"requestId": params.requestId}, (error, result) => {
if(!error || JSON.stringify(error) == "{}") {
console.log(`getResponseBody result: ${JSON.stringify(result)}`)
} else {
console.log(`getResponseBody error: ${JSON.stringify(error)}`)
}
})
}
}
if(method === 'Network.webSocketFrameReceived'){
console.log(params.response)
}
})