docs.rodeo

MDN Web Docs mirror

proxy.onRequest

{{AddonSidebar}} 

Fired when a web request is about to be made, to give the extension an opportunity to proxy it.

This event is closely modeled on the events defined in the webRequest API. Like those events, its addListener() function takes three arguments:

The event is fired before any of the webRequest events for the same request.

When the event is fired, the listener is called with an object containing information about the request. The listener returns a {{WebExtAPIRef("proxy.ProxyInfo")}}  object representing a proxy to use (or an array of {{WebExtAPIRef("proxy.ProxyInfo")}}  objects, enabling the browser to fail over if a proxy is unreachable).

To use proxy.onRequest, an extension must have the “proxy” API permission and the host permission for the URLs of the requests that it intercepts, which means that the match patterns in the filter argument must be a subset of the extension’s host permissions.

Syntax

browser.proxy.onRequest.addListener(
  listener,             //  function
  filter,               //  object
  extraInfoSpec         //  optional array of strings
)
browser.proxy.onRequest.removeListener(listener)
browser.proxy.onRequest.hasListener(listener)

Events have three functions:

addListener syntax

Parameters

Browser compatibility

{{Compat}} 

Examples

This code intercepts requests to <all_urls>, and proxies them if they are not for a top-level frame.

function shouldProxyRequest(requestInfo) {
  return requestInfo.parentFrameId !== -1;
}

function handleProxyRequest(requestInfo) {
  if (shouldProxyRequest(requestInfo)) {
    console.log(`Proxying: ${requestInfo.url}`);
    return { type: "http", host: "127.0.0.1", port: 65535 };
  }
  return { type: "direct" };
}

browser.proxy.onRequest.addListener(handleProxyRequest, {
  urls: ["<all_urls>"],
});

{{WebExtExamples}} 

In this article

View on MDN