user3833308 user3833308 - 1 year ago 272
HTTP Question

Chrome Extension : How to intercept requested urls?

How can an extension intercept any requested URL to block it if some condition matches?

Similar question for Firefox.

What permission needs to be set in manifest.json?

Answer Source

JavaScript Code :

The following example illustrates how to block all requests to

  function(details) {
    return {cancel: details.url.indexOf("://") != -1};
  { urls: ["<all_urls>"] },

The following example achieves the same goal in a more efficient way because requests that are not targeted to do not need to be passed to the extension:

  function(details) { 
    return { cancel: true }; 
  {urls: ["*://*"]},

Registering event listeners:

To register an event listener for a web request, you use a variation on the usual addListener() function. In addition to specifying a callback function, you have to specify a filter argument and you may specify an optional extra info argument.

The three arguments to the web request API's addListener() have the following definitions:

var callback = function(details) {...};
var filter = {...};
var opt_extraInfoSpec = [...];

Here's an example of listening for the onBeforeRequest event:

  callback, filter, opt_extraInfoSpec);

Permission needed on manifest.json :

"permissions": [

Extensions examples and help links:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download