2

What is the simplest way to make the firefox addon, which repeats this chrome functionality:

chrome.webRequest.onBeforeRequest.addListener(
  function(info) {
    if(info.url.indexOf("notifier") + 1){
        return {redirectUrl: "https://domain.null/1.js"};
    }

  },
  {
    urls: [
      "*://domain2.null/*"
    ],
    types: ["script"]
  }, ["blocking"]);

I know about nsIContentPolicy in firefox, but I don't understand how to use it.

All opinions, advice, and help will be appreciated

Answer

I've determined the problem with the restartless extension.
To block content we can use nsIContentPolicy as Wladimir said. We also can inject script to page with windowListener (aWindow.gBrowser).

For example, this practice works perfectly: https://github.com/jvillalobos/AMO-Admin-Assistant/blob/master/src/bootstrap.js

Andrei Nikolaev
  • 113
  • 2
  • 13

1 Answers1

5

I don't think that this can be done without major hacks right now. This is subject of bug 765934 that will add a redirectTo() method to the nsIHttpChannel interface. Once it is implemented code like this should work:

const Ci = Components.interfaces;
const Cu = Components.utils;

Cu.import("resource://gre/modules/Services.jsm");
Cu.import("resource://gre/modules/XPCOMUtils.jsm");

var observer = {
  QueryInterface: XPCOMUtils.generateQI([
    Ci.nsIObserver,
    Ci.nsISupportsWeakReference
  ]),

  observe: function(subject, topic, data)
  {
    if (topic == "http-on-modify-request" &&
        subject instanceof Ci.nsIHttpChannel)
    {
      var uri = subject.URI;
      if (uri.host == "domain2.null" && /\.js(\?|$)/.test(uri.path))
      {
        var redirectUri = Services.io.newURI("https://domain.null/1.js",
                                             null, null);
        subject.redirectTo(redirectUri);
      }
    }
  }
};

Services.obs.addObserver(observer, "http-on-modify-request", true);

For reference: Services.jsm, XPCOMUtils.jsm, observer notifications, nsIHttpChannel, nsIURI

Wladimir Palant
  • 56,865
  • 12
  • 98
  • 126
  • Wladimir. Thank you very much for you quickly answer. I'll try to use it. (Additionally thanks for adblockplus) – Andrei Nikolaev Nov 10 '12 at 10:26
  • Wladimir, can I block loading certain script on webpage by extension? – Andrei Nikolaev Nov 11 '12 at 18:29
  • @AndreiNikolaev: As I said, `redirectTo()` isn't implemented yet so you cannot use it. However, you can already call [`cancel()`](https://developer.mozilla.org/en-US/docs/XPCOM_Interface_Reference/nsIRequest#cancel%28%29) on a channel instead to stop it. Or you can [use content policies](http://stackoverflow.com/a/10788836/785541), you will get better context information then. – Wladimir Palant Nov 12 '12 at 07:07
  • 1
    Oh my God you saved me, It's so hard to work with these API, the documents are not enough I don't really know what to do. I was looking for how to make the redirectURI for hours. – Iman Mohamadi May 03 '14 at 14:16