# webRequest - 监控网络请求

使用 [webRequest](https://developers.chrome.com/extensions/webRequest) API 可以让插件监控 Chrome 的网络请求，甚至重定向请求。

![](https://2936376509-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LJldJ5SQnvMdIyYRWx5%2F-LKMrS3i_HdUBjQC--Y7%2F-LKMrc2setJ670HoKVp8%2Fwebrequest.gif?generation=1534777529868884\&alt=media)

如图，我们可以替换掉原有图片。

## 配置

见 [manifest.json](https://github.com/welearnmore/chrome-extension-demos/blob/master/webrequest/manifest.json#L9-L16)

* webRequest: 开启 webRequest
* webRequestBlocking: 让 webRequest 可以 block 网络请求

> 需要注意的是，使用 webRequest 的时候，`background` 不能设置 `"persistent": false`，因为它需要[时刻监控](https://developer.chrome.com/extensions/background_pages#manifest)。
>
> 还有，webRequest 不能替换 https 资源 !

## 开发

```javascript
// background.js
chrome.webRequest.onBeforeRequest.addListener(
  function (details) {
    const url = details.url;

    if (url === 'http://n.sinaimg.cn/news/1_img/upload/7b577cec/576/w778h598/20180820/lSSg-hhxaafy9194151.jpg') {
      console.log(22)
      return {
        redirectUrl: 'https://github.com/welearnmore/chrome-extension-book/raw/master/doc/images/logo_google_developers.png'
      }
    }

    return {cancel: false}
  },
  {urls: ["<all_urls>"]},
  ["blocking"]
)
```

最后，打开[页面](http://slide.news.sina.com.cn/c/slide_1_86058_312621.html#p=1) 即可看见效果。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://lightningminers.gitbook.io/chrome-extension-book/index-2/webrequest.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
