是否有可能知道一个HTTPRequest的目标DOMWindow? [英] Is it possible to know the target DOMWindow for an HTTPRequest?

查看:127
本文介绍了是否有可能知道一个HTTPRequest的目标DOMWindow?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个firefox扩展,它要求我通过筛选出一些HTTPRequest来拦截页面加载。
我使用了这里给出的说明。请注意,我的问题来自于这个链接的内容。



我使用 HTTPObserver 。它的工作,我确实能够提取相应的请求被发送出去。



然而,我真正需要的另一件事是获得目标DOM窗口其中与HTTPRequest有关的内容即将被加载。是否可以使用HTTPObservers?



在上面的链接中,另一种方法是使用 WebProgressListeners



我也试过了。 onLocationChange()方法只返回url栏中的位置更改。是否有可能得到使用任何这些进度监听器的HTTPRequest网址?因为如果是这样,那么如果我理解正确,aWebProgress.DOMWindow会给我我需要的窗口。



注意:我使用gwt作为扩展名,而JSNI用于上面提到的部分。

解决方案

您通常可以使用 nsILoadContext 接口(遗憾的是没有记录)连接到请求或其负载组。这里是你将如何做到这一点:$ b​​

  function getWindowForRequest(request)
{
if(request instanceofinterface.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;



$ b $ p
$ b

请注意,这个函数预期会返回 null 偶尔 - 并不是每个HTTP请求都与一个窗口关联。


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 that my question draws from the content of this link.

I used the method given under the section of HTTPObservers. And it worked, I am indeed able to extract the respective urls of the Requests being sent out.

However, another thing which I really require is to get the target DOM Window where the contents pertaining to the HTTPRequest were about to be loaded. Is it possible using HTTPObservers?

In the link above, another way has been described using WebProgressListeners.

I tried that out as well. The onLocationChange() method only returns location changes in the url bar. Is it somehow possible to get the HTTPRequest urls using any of these progress listeners? Because if so, then if I understand correctly, aWebProgress.DOMWindow would give me the window I require.

Note: I am using gwt for the extension and the JSNI for the above mentioned part.

解决方案

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.

这篇关于是否有可能知道一个HTTPRequest的目标DOMWindow?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆