如何在特定选项卡上阻止HTTP请求?

问题描述

| 我正在开发FF插件。我想在特定选项卡中阻止除特定域(用户定义的域)以外的任何域的所有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);

}
    

解决方法

        这是一个示例,您可以从请求(应该是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。     

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...