如何区分 Firefox 标签页?

发布于 2024-11-16 20:32:59 字数 295 浏览 5 评论 0原文

我目前正在开发一个 Firefox 扩展,我需要能够区分每个打开的选项卡。

这个想法是能够将传出的 HTTP 请求与其源选项卡相关联。例如,对于观察到的每个“on-modify-request”事件,我知道它来自选项卡 #2 或选项卡 #3。

这必须足以区分同一网站的多个实例。例如,如果我打开了三个“www.google.com”选项卡,则枚举所有打开的选项卡将不起作用。

据我所知,Mozilla 中的 tabbrowser 对象没有任何唯一的标识符或属性。

有什么想法吗?

I am currently developing a Firefox extension, and I need the ability to be able to differentiate between each open tab.

The idea is to be able to associate outgoing HTTP requests with its origin tab. For each 'on-modify-request' event that is observed, for example, I would know that it came from tab #2, or tab #3.

This would have to be good enough to differentiate between multiple instances of the same website. Enumerating through all open tabs will not work if I have three 'www.google.com' tabs open for example.

As far as I know, tabbrowser objects in Mozilla do not have any unique identifiers or properties.

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我不在是我 2024-11-23 20:32:59

http-on-modify-request 为您提供实际的请求对象。从那里您需要获取关联的窗口,方法是 nsILoadContext 此处。我使用这段代码:

function getRequestWindow(request)
{
  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;
}

这是一个内容窗口(甚至可能是一个框架)。所以你必须找到相应的选项卡 - 使用 gBrowser.getBrowserForDocument() 。像这样:

var wnd = getRequestWindow(request);
var browser = (wnd ? gBrowser.getBrowserForDocument(wnd.top.document) : null);

现在您有了请求所属的 元素 - 如果有的话。因为请求也可能来自 Firefox UI 或其他浏览器窗口。您可以在该元素上设置自己的 Expando 属性来获取选项卡标识符(选择唯一的名称以避免与其他扩展发生冲突)。像这样:

if (!("_myExtensionTabId" in browser))
    browser._myExtensionTabId = ++maxTabId;
var tabId = browser._myExtensionTabId;

这里的 maxTabId 是一个初始为零的全局变量。理想情况下,“myExtension”应替换为您的扩展所特有的内容(例如其名称)。

http-on-modify-request gives you the actual request object. From there you need to get the associated window, the way to go is nsILoadContext here. I use this code:

function getRequestWindow(request)
{
  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;
}

That's a content window (and probably even a frame). So you have to find the corresponding tab - use gBrowser.getBrowserForDocument(). Like this:

var wnd = getRequestWindow(request);
var browser = (wnd ? gBrowser.getBrowserForDocument(wnd.top.document) : null);

Now you have the <browser> element that the request belongs to - if any. Because the request might also originate from the Firefox UI or a different browser window. You can set your own expando property on that element to get a tab identifier (choose a unique name to avoid conflicts with other extensions). Something like this:

if (!("_myExtensionTabId" in browser))
    browser._myExtensionTabId = ++maxTabId;
var tabId = browser._myExtensionTabId;

Here maxTabId would be a global variable that's initially zero. And "myExtension" would be ideally replaced by something unique to your extension (e.g. its name).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文