2

In my Firefox extension I want to intercept the url that the browser is requesting and block the request entirely if some condition matches

How can I intercept URL being requested?

intika
  • 8,448
  • 5
  • 36
  • 55
user3833308
  • 1,132
  • 3
  • 14
  • 39
  • 3
    I think best option is is nsIContentPolicy but you can also use observer service: http://stackoverflow.com/a/25328750/1828637 – Noitidart Jun 02 '15 at 00:30

2 Answers2

5

you can have a look at the source of those addons

https://addons.mozilla.org/en-us/firefox/addon/blocksite/?src=search https://addons.mozilla.org/en-us/firefox/addon/url-n-extension-blockune-bl/?src=search

or use service observer with nsIHTTPChannel for fast handling

const { Ci, Cu, Cc, Cr } = require('chrome'); //const {interfaces: Ci, utils: Cu, classes: Cc, results: Cr } = Components;
Cu.import('resource://gre/modules/Services.jsm');
Cu.import('resource://gre/modules/devtools/Console.jsm');

var observers = {
    'http-on-modify-request': {
        observe: function (aSubject, aTopic, aData) {
            console.info('http-on-modify-request: aSubject = ' + aSubject + ' | aTopic = ' + aTopic + ' | aData = ' + aData);
            var httpChannel = aSubject.QueryInterface(Ci.nsIHttpChannel);
            var requestUrl = httpChannel.URI.spec
            if (requestUrl.indexOf('google.com') > -1) {
               //httpChannel.cancel(Cr.NS_BINDING_ABORTED); //this aborts the load
               httpChannel.redirectTo(Services.io.newURI('data:text,url_blocked', null, null)); //can redirect with this line, if dont want to redirect and just block, then uncomment this line and comment out line above (line 17)
            }
        },
        reg: function () {
            Services.obs.addObserver(observers['http-on-modify-request'], 'http-on-modify-request', false);
        },
        unreg: function () {
            Services.obs.removeObserver(observers['http-on-modify-request'], 'http-on-modify-request');
        }
    }
};

To start observing

To start start obseving all requests do this (for example on startup of your addon)

for (var o in observers) {
    observers[o].reg();
}

To stop observing

Its important to stop observring (make sure to run this at least on shutdown of addon, you dont want to leave the observer registered for memory reasons)

for (var o in observers) {
    observers[o].unreg();
}

Full working example of the observer service to block/redirect urls: https://github.com/Noitidart/PortableTester/tree/block-urls

intika
  • 8,448
  • 5
  • 36
  • 55
  • 1
    Thanks, I created a basic addon with `cxf init` and replaced `main.js` with `bootstrap.js` and I get this http://pastebin.com/uZ4bbrsp I am very new to this – user3833308 Jun 02 '15 at 05:49
  • 1
    firefox version `38.0.1` and addon-sdk version `1.17` – user3833308 Jun 02 '15 at 06:16
  • best way is to take an existing addon and modify it – intika Jun 02 '15 at 06:26
  • 1
    agree, however `cannot find 'package.json' in the current directory or any parent.` fails with this error, so I just created this basic template and added your bootstrap.js to main.js, how to hook these things together, Thank you – user3833308 Jun 02 '15 at 06:28
  • 1
    also firefox debugger should be useful to debug your addon to open it (ctrl + shift + j) also you have to enable debugging for addons you could do it easily with https://addons.mozilla.org/en-us/firefox/addon/devprefs/?src=search – intika Jun 02 '15 at 06:29
  • 1
    I think I don't understand entry point of the addon, yes I have debugger setup but it never goes to your startup() method where it reigsters all the observers – user3833308 Jun 02 '15 at 06:31
  • 1
    here is a simple addon that use nsIHTTPChannel ... download the xpi and extract it – intika Jun 02 '15 at 06:31
  • 1
    you did not took an easy stuff to start with on ffox.. good luck but you can pm me as i am working on that for my current addon – intika Jun 02 '15 at 06:36
  • 1
    do you happen to have template for chrome as well ? – user3833308 Jun 02 '15 at 07:14
  • 1
    Thanks for helping out @intika :) – Noitidart Jun 02 '15 at 08:30
3

An other possible solution :

Here is an other implementation as modules example from HTTPS-Everywhere

Init function :

  init: function() {
    // start observing all http requests
    Services.obs.addObserver(httpNowhere, "http-on-modify-request", false);
  },

Observer function :

observe: function(subject, topic, data) {
var request = subject.QueryInterface(Ci.nsIHttpChannel);
  if (topic == "http-on-modify-request") {
    if (request.URI.spec == "xxx.example.com") {
      request.redirectTo("yyy.example.com");
    }
    else {
      request.cancel(Components.results.NS_ERROR_ABORT);
    }
  }
},

Example addons :

HTTPS-Nowhere - https://github.com/cwilper/http-nowhere

HTTPS-Everywhere - https://github.com/EFForg/https-everywhere

Migrating your extension to chrome :

i answered your question for chrome in this page : Chrome Extension : How to intercept requested urls?

Community
  • 1
  • 1
intika
  • 8,448
  • 5
  • 36
  • 55
  • 1
    can't accept two answers, please post it here http://stackoverflow.com/questions/30590428/chrome-extension-how-to-intercept-url-being-requested – user3833308 Jun 02 '15 at 08:02