webRequest

Add event listeners for the various stages of making an HTTP request, which includes websocket requests on ws:// and wss:// . The event listener receives detailed information about the request and can modify or cancel the request.

Each event is fired at a particular stage of the request. The typical sequence of events is like this:

onErrorOccurred can be fired at any time during the request. Also, note that sometimes the sequence of events may differ from this: for example, in Firefox, on an HSTS upgrade, the onBeforeRedirect event will be triggered immediately after onBeforeRequest .

All events— except onErrorOccurred —can take three arguments to addListener() :

  • the listener itself
  • a filter object, so you can only be notified for requests made to particular URLs or for particular types of resource
  • an optional extraInfoSpec object. You can use this to pass additional event-specific instructions.

The listener function is passed a details object containing information about the request. This includes a request ID, which is provided to enable an add-on to correlate events associated with a single request. It is unique within a browser session and the add-on's context. It stays the same throughout a request, even across redirections and authentication exchanges.

要使用 webRequest API for a given host, an extension must have the "webRequest" API permission host permission for that host. To use the "blocking" feature, the extension must also have the "webRequestBlocking" API permission.

To intercept resources loaded by a page (such as images, scripts, or stylesheets), the extension must have the host permission for the resource as well as for the main page requesting the resource. For example, if a page at https://developer.mozilla.org loads an image from https://mdn.mozillademos.org , then an extension must have both host permissions if it is to intercept the image request.

Modifying requests

On some of these events, you can modify the request. Specifically, you can:

To do this, you need to pass an option with the value "blocking" extraInfoSpec argument to the event's addListener() . This makes the listener synchronous.

In the listener, you can then return a BlockingResponse object, which indicates the modification you need to make: for example, the modified request header you want to send.

Accessing security information

onHeadersReceived listener you can access the TLS properties of a request by calling getSecurityInfo() . To do this you must also pass "blocking" in the extraInfoSpec argument to the event's addListener() .

You can read details of the TLS handshake, but can't modify them or override the browser's trust decisions.

Modifying responses

To modify the HTTP response bodies for a request, call webRequest.filterResponseData , passing it the ID of the request. This returns a webRequest.StreamFilter object that you can use to examine and modify the data as it is received by the browser.

To do this, you must have the "webRequestBlocking" API permission as well as the "webRequest" API permission host permission for the relevant host.

类型

webRequest.BlockingResponse

An object of this type is returned by event listeners that have set "blocking" in their extraInfoSpec argument. By setting particular properties in BlockingResponse , the listener can modify network requests.

webRequest.CertificateInfo

An object describing a single X.509 certificate.

webRequest.HttpHeaders

An array of HTTP headers. Each header is represented as an object with two properties: 名称 and either value or binaryValue .

webRequest.RequestFilter

An object describing filters to apply to webRequest 事件。

webRequest.ResourceType

Represents a particular kind of resource fetched in a web request.

webRequest.SecurityInfo

An object describing the security properties of a particular web request.

webRequest.StreamFilter

An object that can be used to monitor and modify HTTP responses while they are being received.

webRequest.UploadData

Contains data uploaded in a URL request.

特性

webRequest.MAX_HANDLER_BEHAVIOR_CHANGED_CALLS_PER_10_MINUTES

The maximum number of times that handlerBehaviorChanged() can be called in a 10 minute period.

方法

webRequest.handlerBehaviorChanged()

This method can be used to ensure that event listeners are applied correctly when pages are in the browser's in-memory cache.

webRequest.filterResponseData()

返回 webRequest.StreamFilter object for a given request.

webRequest.getSecurityInfo()

Gets detailed information about the TLS connection associated with a given request.

事件

webRequest.onBeforeRequest

Fired when a request is about to be made, and before headers are available. This is a good place to listen if you want to cancel or redirect the request.

webRequest.onBeforeSendHeaders

Fired before sending any HTTP data, but after HTTP headers are available. This is a good place to listen if you want to modify HTTP request headers.

webRequest.onSendHeaders

Fired just before sending headers. If your add-on or some other add-on modified headers in onBeforeSendHeaders , you'll see the modified version here.

webRequest.onHeadersReceived

Fired when the HTTP response headers associated with a request have been received. You can use this event to modify HTTP response headers.

webRequest.onAuthRequired

Fired when the server asks the client to provide authentication credentials. The listener can do nothing, cancel the request, or supply authentication credentials.

webRequest.onResponseStarted

Fired when the first byte of the response body is received. For HTTP requests, this means that the status line and response headers are available.

webRequest.onBeforeRedirect

Fired when a server-initiated redirect is about to occur.

webRequest.onCompleted

Fired when a request is completed.

webRequest.onErrorOccurred

Fired when an error occurs.

浏览器兼容性

BCD tables only load in the browser

Additional notes on Chrome incompatibilities .

Example extensions

注意: This API is based on Chromium's chrome.webRequest API. This documentation is derived from web_request.json in the Chromium code.

Microsoft Edge compatibility data is supplied by Microsoft Corporation and is included here under the Creative Commons Attribution 3.0 United States License.

Found a problem with this page?

最后修改: , 由 MDN 贡献者

  1. 浏览器扩展名
  2. 快速入门
    1. What are extensions?
    2. Your first extension
    3. Your second extension
    4. Anatomy of an extension
    5. Example extensions
    6. What next?
  3. 概念
    1. Using the JavaScript APIs
    2. Content scripts
    3. Match patterns
    4. Working with files
    5. 国际化
    6. Content Security Policy
    7. Native messaging
    8. Differences between API implementations
    9. Chrome incompatibilities
  4. 用户界面
    1. 用户界面
    2. Toolbar button
    3. Address bar button
    4. Sidebars
    5. Context menu items
    6. Options page
    7. Extension pages
    8. Notifications
    9. Address bar suggestions
    10. Developer tools panels
  5. 如何
    1. Intercept HTTP requests
    2. Modify a web page
    3. Insert external content
    4. Share objects with page scripts
    5. Add a button to the toolbar
    6. Implement a settings page
    7. Work with the Tabs API
    8. Work with the Bookmarks API
    9. Work with the Cookies API
    10. Work with contextual identities
    11. Interact with the clipboard
    12. Build a cross-browser extension
  6. Firefox differentiators
  7. JavaScript API
    1. Browser support for JavaScript APIs
    2. alarms
    3. bookmarks
    4. browserAction
    5. browserSettings
    6. browsingData
    7. captivePortal
    8. clipboard
    9. 命令
    10. contentScripts
    11. contextualIdentities
    12. Cookie
    13. devtools
    14. dns
    15. downloads
    16. events
    17. extension
    18. extensionTypes
    19. find
    20. history
    21. i18n
    22. identity
    23. idle
    24. management
    25. menus
    26. notifications
    27. omnibox
    28. pageAction
    29. permissions
    30. pkcs11
    31. privacy
    32. proxy
    33. runtime
    34. search
    35. sessions
    36. sidebarAction
    37. storage
    38. tabs
    39. theme
    40. topSites
    41. 类型
    42. userScripts
    43. webNavigation
    44. webRequest
      1. 方法
        1. filterResponseData()
        2. getSecurityInfo()
        3. handlerBehaviorChanged()
      2. 特性
        1. MAX_HANDLER_BEHAVIOR_CHANGED_CALLS_PER_10_MINUTES
      3. 类型
        1. BlockingResponse
        2. CertificateInfo
        3. HttpHeaders
        4. RequestFilter
        5. ResourceType
        6. SecurityInfo
        7. StreamFilter
        8. UploadData
      4. 事件
        1. onAuthRequired
        2. onBeforeRedirect
        3. onBeforeRequest
        4. onBeforeSendHeaders
        5. onCompleted
        6. onErrorOccurred
        7. onHeadersReceived
        8. onResponseStarted
        9. onSendHeaders
    45. windows
  8. Manifest keys
    1. 介绍
    1. 作者
    2. background
    3. browser_action
    4. browser_specific_settings
    5. chrome_settings_overrides
    6. chrome_url_overrides
    7. 命令
    8. content_scripts
    9. content_security_policy
    10. default_locale
    11. description
    12. developer
    13. devtools_page
    14. dictionaries
    15. externally_connectable
    16. homepage_url
    17. icons
    18. incognito
    19. manifest_version
    20. name
    21. offline_enabled
    22. omnibox
    23. optional_permissions
    24. options_page
    25. options_ui
    26. page_action
    27. permissions
    28. protocol_handlers
    29. short_name
    30. sidebar_action
    31. storage
    32. theme
    33. theme_experiment
    34. user_scripts
    35. version
    36. version_name
    37. web_accessible_resources
  9. Extension Workshop
    1. Develop
    2. Publish
    3. Manage
    4. Enterprise
  10. Contact us
  11. Channels
    1. Add-ons blog
    2. Add-ons forum
    3. Add-ons chat