Is it possible to know the target DOMWindow for an HTTPRequest?

后端 未结 1 2075
旧巷少年郎
旧巷少年郎 2020-12-17 04:25

I\'m developing a firefox extension which requires me to intercept page loads by filtering out some HTTPRequests. I did that using the instructions given here. Please note t

相关标签:
1条回答
  • 2020-12-17 04:56

    You can usually do that by using nsILoadContext interface (sadly barely documented) attached to the request or its load group. Here is how you would do that:

    function getWindowForRequest(request)
    {
      if (request instanceof Components.interfaces.nsIRequest)
      {
        try
        {
          if (request.notificationCallbacks)
          {
            return request.notificationCallbacks
                          .getInterface(Components.interfaces.nsILoadContext)
                          .associatedWindow;
          }
        } catch(e) {}
    
        try
        {
          if (request.loadGroup && request.loadGroup.notificationCallbacks)
          {
            return request.loadGroup.notificationCallbacks
                          .getInterface(Components.interfaces.nsILoadContext)
                          .associatedWindow;
          }
        } catch(e) {}
      }
    
      return null;
    }
    

    Note that this function is expected to return null occasionally - not every HTTP request is associated with a window.

    0 讨论(0)
提交回复
热议问题