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

用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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,562评论 19 139
  • =========================================================...
    lavor阅读 8,836评论 0 5
  • 《夏目友人帐》一直列在心中好看动漫清单里,画风优美,由一个个夏目、猫咪老师和神妖的小故事串联而成,温馨治愈。记得以...
    一条红烧鱼呦阅读 1,651评论 4 0
  • 8.6 这几天听语音受同修和李悦老师的启发,今天专程去悲田种种子去了。早晨起来特别开心叫儿子穿好衣服准备出发,儿子...
    韩艾辰阅读 1,644评论 0 0
  • 以下纯是看后,停留在脑子里的瞬间 一.单纯的想法 友好的哑巴人,小时候的我太软弱了,没能反抗老师,错过了那份好意。...
    一直在变的风阅读 2,895评论 0 1