How to block HTTP request on a particular tab?
I'm developing an FF addon. I want to block all the http request of any domain except a particular domain (user defined domain) in a particular tab. The function given below do the job well. But the problem is that it blocks http request from all the tabs. How to enable to below function only in a particular tab? how to get the tab associated with the 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)
开发者_StackOverflow社区 {
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);
}
Here is a example that you can get the loadContent from a request( which should be a 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 "associatedWindow", "topWindow" property, so you should get the source DOMWindow.
精彩评论