--- title: proxy slug: Mozilla/Add-ons/WebExtensions/API/proxy translation_of: Mozilla/Add-ons/WebExtensions/API/proxy ---
{{AddonSidebar}}

Warning
不推荐使用 {{WebExtAPIRef("proxy.register()")}} 或者 {{WebExtAPIRef("proxy.unregister()")}} 方法使用 Proxy Auto-Configuration (PAC) file. 这个 API 将会在 Firefox 68 中废弃并且在 Firefox 71中删除.

使用proxy API来代理web请求。你可以使用{{WebExtAPIRef("proxy.onRequest")}}事件监听器来拦截web请求,并且返回一个可以描述是否代理并且怎样代理它们的对象。

{{WebExtAPIRef("proxy.onRequest")}}的好处在于,用于实现你拦截策略的代码在你的扩展的后台脚本运行,所以,它可以让WebExtension APIs能够完全的访问你的扩展(举例来说,可以访问你扩展的storage和像dns等类似的网络api)

除了这个api,扩展也能够使用browserSettings.proxyConfig属性来重新编辑你全局的proxy设置

Chrome浏览器提供了一个叫做'proxy'api扩展,它的功能跟这个api类似,在chrome的api中也可以用来实现一个拦截策略。然而,Chrome 的API的设计跟这个API设计完全不同。因为这个API跟谷歌的proxyAPI完全不同, 这个API只能通过'browser'命名空间访问

如果你想用这个API你需要得到'proxy'的permission.并且,如果你想拦截一个请求,你同样也需要当前拦截请求的url的 host permission

Types

{{WebExtAPIRef("proxy.ProxyInfo")}}
Describes a proxy.
{{WebExtAPIRef("proxy.RequestDetails")}}

Contains information about a web request that the browser is about to make.

Properties

{{WebExtAPIRef("proxy.settings")}}
Get and set proxy settings.

Functions

{{WebExtAPIRef("proxy.register()")}} {{Deprecated_Inline}}
Registers the given proxy script.
{{WebExtAPIRef("proxy.unregister()")}} {{Deprecated_Inline}}
Unregisters the proxy script.

Events

{{WebExtAPIRef("proxy.onError")}}
Fired when the system encounters an error running the PAC script or the onRequest listener.
{{WebExtAPIRef("proxy.onRequest")}}
Fired when a web request is about to be made, giving the extension an opportunity to proxy it.

{{WebExtExamples("h2")}}

Browser compatibility

{{Compat("webextensions.api.proxy")}}