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

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

问题描述

我正在开发一个 Firefox 扩展,它要求我通过过滤掉一些 HTTPRequest 来拦截页面加载.我使用此处给出的说明做到了这一点.请注意,我的问题来自此链接的内容.

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.

我使用了 HTTPObservers 部分中给出的方法.它奏效了,我确实能够提取发出的请求的相应网址.

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.

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

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?

在上面的链接中,使用 WebProgressListeners 描述了另一种方式.

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

我也试过了.onLocationChange() 方法仅返回 url 栏中的位置更改.是否可以使用任何这些进度侦听器以某种方式获取 HTTPRequest url?因为如果是这样,那么如果我理解正确,aWebProgress.DOMWindow 会给我我需要的窗口.

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.

注意:我将 gwt 用于扩展,而 JSNI 用于上述部分.

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

推荐答案

您通常可以通过使用 nsILoadContext 接口(遗憾的是几乎没有记录)附加到请求或其负载组.您可以这样做:

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

请注意,此函数偶尔会返回 null - 并非每个 HTTP 请求都与窗口相关联.

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

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

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