简体   繁体   English

如何使用 chrome-extension 收听 xhr 响应?

[英]How can I listen to xhr response using chrome-extension?

I want execute a function when some a chrome window gets a XHR response.我想在某个 chrome 窗口收到 XHR 响应时执行一个函数。

I don't know what exactly this request is like, because of a codified param of this request, for example: api.xxx.com/rest?random=123我不知道这个请求到底是什么样的,因为这个请求的编码参数,例如: api.xxx.com/rest?random=123

So I don't think I could use所以我不认为我可以使用

chrome.devtools.network.onRequestFinished.addListener(function callback)

or

chrome.webRequest.onCompleted.addListener(function callback)

which both need specify the request details.两者都需要指定请求详细信息。

The listeners for those events do not need you to specify the request details.这些事件的侦听器不需要您指定请求详细信息。 On the contrary, they provide you with those details, when the get called.相反,他们会在被调用时为您提供这些详细信息。

Since you want to listen for any XHR request, you can define the special <all_urls> match pattern (or *://*/* to limit them to just http/https requests).由于您想侦听任何 XHR 请求,您可以定义特殊的<all_urls>匹配模式(或*://*/*将它们限制为http/https请求)。

Eg:例如:

chrome.webRequest.onCompleted.addListener(function (details) {
  // Process the XHR response.
  ...
}, {urls: ['<all_urls>']});

Don't forget to declare the appropriate permissions, according to your requirements.不要忘记根据您的要求声明适当的权限。
Eg:例如:

// In `manifest.json`:
...
"permissions": {
  ...
  "webRequest",
  "<all_urls>"   // <-- add this to listen for XHR from all pages
]

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

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