proxy.register()

Deprecated: This feature is no longer recommended. Though some browsers might still support it, it may have already been removed from the relevant web standards, may be in the process of being dropped, or may only be kept for compatibility purposes. Avoid using it, and update existing code if possible; see the 兼容性表格 at the bottom of this page to guide your decision. Be aware that this feature may cease to work at any time.

警告: This method was deprecated in Firefox 68 and removed in Firefox 71. In Firefox 68–70, calling this method logs an error message to the console:

注册 Proxy Auto-Configuration (PAC) file . The file is executed immediately, and its FindProxyForURL() function will be called for any HTTP or HTTPS requests.

If PAC files are registered by more than one extension, then requests will be passed initially to the one that was registered first.

  • FindProxyForURL() function in the first PAC returns "DIRECT" for a request, then the request will be passed unchanged to the FindProxyForURL() function in the next PAC.
  • FindProxyForURL() function in the first PAC proxies the request by returning "PROXY" or some other proxying value, then the proxy URL will be passed to the FindProxyForURL() function in the next PAC.

Each extension can only register a single PAC file: if you call register() twice, the second PAC file will replace the first.

This is an asynchronous function that returns a Promise .

Communicating with PAC files

You can exchange messages between the PAC file and your extension's background page (or any other privileged pages, like popup pages) using runtime.sendMessage() and runtime.onMessage .

To send a message to the PAC file, you must set the toProxyScript 选项:

// background.js
// Log any messages from the proxy.
browser.runtime.onMessage.addListener((message, sender) => {
  if (sender.url === browser.extension.getURL(proxyScriptURL)) {
    console.log(message);
  }
});
let messageToProxy = {
  enabled: true,
  foo: "A string",
  bar: 1234
};
browser.runtime.sendMessage(messageToProxy, {toProxyScript: true});

							
// pac.js
browser.runtime.onMessage.addListener((message) => {
  if (message.enabled) {
    browser.runtime.sendMessage("I'm enabled!");
  }
});

							

PAC file specification

The basic PAC file syntax is described in the PAC documentation , but the implementation used by the proxy API differs from standard PAC design in several ways, which are described in this section.

FindProxyForURL() return value

标准 FindProxyForURL() returns a string . In Firefox 55 and 56, the PAC file used with the proxy API also returns a string. In Firefox 55 only , you must pass an argument to the "DIRECT" return value, even though it doesn't need an argument.

From Firefox 57 onwards,  FindProxyForURL() may still return a string, but may alternatively (and preferably) return an array of proxy.ProxyInfo 对象。

If the array contains more than one object, then all ProxyInfo objects after the first one represent failovers: if the proxy at position N in the array is not reachable when its ProxyInfo.failoverTimeout expires, then the browser will try the proxy at position N+1.

例如:

const proxySpecification = [
  {
    type: "socks",
    host: "foo.com",
    port: 1080,
    proxyDNS: true,
    failoverTimeout: 5
  },
  {
    type: "socks",
    host: "bar.com",
    port: 1060,
  }
];

							

The first proxy in the array will be tried first. If it does not respond in failoverTimeout seconds, the next will be tried, until the end of the array is reached.

PAC file environment

The global helper functions usually available for PAC files ( isPlainHostName() , dnsDomainIs() , and so on) are not available.

Code running in the PAC file does not get access to:

//  pac.js
// send the log message to the background script
browser.runtime.sendMessage(`Proxy-blocker: blocked ${url}`);

							
// background-script.js
function handleMessage(message, sender) {
  // only handle messages from the proxy script
  if (sender.url != browser.extension.getURL(proxyScriptURL)) {
    return;
  }
  console.log(message);
}
browser.runtime.onMessage.addListener(handleMessage);

							

句法

var registering = browser.proxy.register(
  url   // string
)

							

参数

url

字符串 . URL pointing to the PAC file to load. PAC files must be bundled with the extension, and url must be relative to the extension's manifest.json 文件。

返回值

A Promise that will be fulfilled with no arguments when the PAC file has been registered, or rejected if there was an error.

范例

const proxyScriptURL = "proxy/proxy-script.js";
浏览器

.

proxy

.


register


(

proxyScriptURL

)


;


							

浏览器兼容性

BCD tables only load in the browser

注意: 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
      1. 方法
        1. register()
        2. unregister()
      2. 特性
        1. settings
      3. 类型
        1. ProxyInfo
        2. RequestDetails
      4. 事件
        1. onError
        2. onRequest
    33. runtime
    34. search
    35. sessions
    36. sidebarAction
    37. storage
    38. tabs
    39. theme
    40. topSites
    41. 类型
    42. userScripts
    43. webNavigation
    44. webRequest
    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

版权所有  © 2014-2026 乐数软件    

工业和信息化部: 粤ICP备14079481号-1