Firefox extension: url interception requests and blocks conditionally

in the firefox extension. I want to intercept the url browser that makes the request and completely blocks the request if any condition matches

how can i intercept the requested url

+2
javascript firefox firefox-addon firefox-addon-sdk
source share
2 answers

you can see the source of these 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 a service observer with nsIHTTPChannel for fast processing

 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 the observation

To start the survey, all requests do this (for example, when you start your addon)

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

To stop watching

It is important to stop the observation (be sure to run this, at least when the add-on is turned off, you will not want to leave the observer registered for memory reasons)

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

Full working example of an observer service for blocking / redirecting URLs: https://github.com/Noitidart/PortableTester/tree/block-urls

+5
source share

Another possible solution:

Here is another example implementation of modules 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); } } }, 

Examples of additions:

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 on this page: Chrome extension: how to intercept the requested URLs?

+3
source share

All Articles