最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How can I listen to xhr response using chrome-extension? - Stack Overflow

programmeradmin0浏览0评论

I want execute a function when some a chrome window gets a XHR response.

I don't know what exactly this request is like, because of a codified param of this request, for example: api.xxx/rest?random=123

So I don't think I could use

chrome.devtoolswork.onRequestFinished.addListener(function callback)

or

chrome.webRequest.onCompleted.addListener(function callback)

which both need specify the request details.

I want execute a function when some a chrome window gets a XHR response.

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

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.

Share Improve this question edited Feb 23, 2020 at 21:05 d-_-b 23.2k43 gold badges171 silver badges282 bronze badges asked Nov 30, 2013 at 13:44 Chris KongChris Kong 3991 gold badge4 silver badges18 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 16

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).

E.g.:

chrome.webRequest.onCompleted.addListener(function (details) {
  // Process the XHR response metadata. The request body is not available
  ...
}, {urls: ['<all_urls>']});

Don't forget to declare the appropriate permissions, according to your requirements.
E.g.:

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

评论列表(0)

  1. 暂无评论