简体   繁体   English

如何在 NodeJS 脚本中访问 Chrome Devtools 网络日志数据?

[英]How can I access Chrome Devtools Network Logs data in NodeJS script?

I'm currently trying to build a project that requires me to read data from a website (OpenSea marketplace).我目前正在尝试构建一个需要我从网站(OpenSea 市场)读取数据的项目。 When I go to the page, it sends a POST request to an API (in pic 1), I want to access the data it returns (in pic 2) in my NodeJS/TypeScript project.当我转到该页面时,它会向 API 发送一个 POST 请求(在图 1 中),我想在我的 NodeJS/TypeScript 项目中访问它返回的数据(在图 2 中)。

Is there maybe an API that can do this or is it not even possible?是否有一个 API 可以做到这一点,或者甚至不可能?

Pic 1 (I'm trying to access the graphql/ request) :图 1 (我正在尝试访问 graphql/ 请求)

在此处输入图像描述

Pic 2 (This is the data I'm trying to get into my code) :图 2 (这是我试图进入我的代码的数据)

在此处输入图像描述

you can hijack the XMLHttpRequest and fetch functions.您可以劫持XMLHttpRequestfetch函数。 unconventional, but it would work非常规,但它会工作

Based on the comment I placed, a small function can be used on the client side to do such根据我发表的评论,可以在客户端使用一个小功能来做到这一点

function listen(fn){
  //this line simply adds functions to call in an array based on setup below
  if(listen.prototype.arguments){listen.prototype.arguments.push(fn)}
  
  //below is setup(functions hijacked so services that use them report to this too)
  listen.prototype.arguments=[] //array of functions to call
  let original1=XMLHttpRequest.prototype.send, original2=fetch
  function replace1(){
    let toReturn=original1.bind(this)(...arguments)
    this.addEventListener("load",res=>fn(this.responseText))
    return toReturn
  }
  function replace2(){
    let toReturn=original2.bind(this)(...arguments)
    toReturn.then(res=>res.text().then(fn))
    return toReturn
  }
  Object.defineProperty(XMLHttpRequest.prototype,"send",{value:replace1})
  Object.defineProperty(window,"fetch",{value:replace2})
}



//example usage
let socket=some_Web_Socket_Your_Backend_To_Handle_This_Data
//perhaps this is not the most elaborate example but I hope the point is understood
listen(socket.send.bind(socket))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM