How to block an HTTP request on a specific tab?

I am developing an FF addon. I want to block the entire HTTP request of any domain except a specific domain (a user-defined domain) on a specific tab. The function below does this job well. But the problem is that it blocks the HTTP request from all tabs. How to enable the function below only on a specific tab? how to get tab associated with http request?

function allowOnly(domain)
{
    //to block http request
    Components.classes["@mozilla.org/observer-service;1"]
    .getService(Components.interfaces.nsIObserverService)
    .addObserver(
    {
    observe:
        function(aSubject, aTopic, aData)
        {
            if ("http-on-modify-request" == aTopic)
            {
                    var url = aSubject
                    .QueryInterface(Components.interfaces.nsIHttpChannel)
                    .originalURI.spec;
                    if (domain.lastIndexOf(doc.location) != 0 ) //cancel all http request of other domain & sub domain
                {
                    aSubject.cancel(Components.results.NS_BINDING_SUCCEEDED);
                    }
                }
        }
    }, "http-on-modify-request", false);

}
+2
source share
1 answer

Here is an example that you can get loadContent from the request (which should be nsIChannel).

var loadContext;
try {
  loadContext = 
    aRequest.QueryInterface(Components.interfaces.nsIChannel)
            .notificationCallbacks
            .getInterface(Components.interfaces.nsILoadContext);
} catch (ex) {
  try {
    loadContext =
      aRequest.loadGroup.notificationCallbacks
              .getInterface(Components.interfaces.nsILoadContext);
  } catch (ex) {
    loadContext = null;
  }
}

And nsILoadContext has the property "relatedWindow", "topWindow", so you should get the original DOMWindow.

+2
source

All Articles