Modify URL before loading page in firefox

lszrh picture lszrh · Mar 5, 2011 · Viewed 7.4k times · Source

I want to prefix URLs which match my patterns. When I open a new tab in Firefox and enter a matching URL the page should not be loaded normally, the URL should first be modified and then loading the page should start.

Is it possible to modify an URL through a Mozilla Firefox Addon before the page starts loading?

Answer

Wayne picture Wayne · Mar 5, 2011

Browsing the HTTPS Everywhere add-on suggests the following steps:

  1. Register an observer for the "http-on-modify-request" observer topic with nsIObserverService
  2. Proceed if the subject of your observer notification is an instance of nsIHttpChannel and subject.URI.spec (the URL) matches your criteria
  3. Create a new nsIStandardURL
  4. Create a new nsIHttpChannel
  5. Replace the old channel with the new. The code for doing this in HTTPS Everywhere is quite dense and probably much more than you need. I'd suggest starting with chrome/content/IOUtils.js.

Note that you should register a single "http-on-modify-request" observer for your entire application, which means you should put it in an XPCOM component (see HTTPS Everywhere for an example).

The following articles do not solve your problem directly, but they do contain a lot of sample code that you might find helpful: