2011-05-29 32 views
2

我正在开发一个FF插件。我想阻止除特定选项卡中的特定域(用户定义的域)以外的任何域的所有http请求。下面给出的功能很好地完成了这项工作。但问题是它阻止来自所有标签的http请求。如何仅在特定标签中启用以下功能?如何获得与http请求关联的选项卡?如何阻止特定选项卡上的HTTP请求?

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); 

} 
+0

再一次...任何hep? – 2011-05-29 11:13:39

回答

2

下面是一个示例,您可以从请求(应该是nsIChannel)获取loadContent。

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; 
    } 
} 

而nsILoadContext有“associatedWindow”,“topWindow”属性,所以你应该得到源DOMWindow。