用electron得到请求页面时XHR的ResponseBody

利用electron访问一些页面时,想要获取到异步加载的内容。
如果利用webContents监听事件did-get-response-details,只能得到以下信息:

  • event Event
  • status Boolean
  • newURL String
  • originalURL String
  • httpResponseCode Integer
  • requestMethod String
  • referrer String
  • headers Object
  • resourceType String

并没有想要的response body。
经过请教与搜索之后,利用Debugger结合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)
      }
  })

Reference

  • https://electron.atom.io/docs/api/debugger/
  • https://chromedevtools.github.io/debugger-protocol-viewer/tot/Network/

你可能感兴趣的:(用electron得到请求页面时XHR的ResponseBody)