简体   繁体   English

如何区分Firefox标签?

[英]How to differentiate between Firefox tabs?

I am currently developing a Firefox extension, and I need the ability to be able to differentiate between each open tab. 我目前正在开发Firefox扩展程序,并且需要能够区分每个打开的选项卡的功能。

The idea is to be able to associate outgoing HTTP requests with its origin tab. 这个想法是为了能够将传出的HTTP请求与其原始选项卡相关联。 For each ' on-modify-request ' event that is observed, for example, I would know that it came from tab #2, or tab #3. 例如,对于观察到的每个“ on-modify-request ”事件,我都知道它来自选项卡#2或选项卡#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. 例如,如果我打开了三个“ www.google.com”标签,则无法枚举所有打开的标签。

As far as I know, tabbrowser objects in Mozilla do not have any unique identifiers or properties. 据我所知,Mozilla中的tabbrowser对象没有任何唯一的标识符或属性。

Any ideas? 有任何想法吗?

http-on-modify-request gives you the actual request object. http-on-modify-request为您提供实际的请求对象。 From there you need to get the associated window, the way to go is nsILoadContext here. 从那里您需要获取关联的窗口,此处的方法是nsILoadContext 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() . 因此,您必须找到相应的选项卡-使用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. 现在,您拥有请求所属的<browser>元素-如果有的话。 Because the request might also originate from the Firefox UI or a different browser window. 因为该请求也可能来自Firefox UI或其他浏览器窗口。 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). 您可以在该元素上设置自己的expando属性以获取选项卡标识符(选择唯一的名称以避免与其他扩展名冲突)。 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. 在这里, maxTabId将是最初为零的全局变量。 And "myExtension" would be ideally replaced by something unique to your extension (eg its name). 理想情况下,“ myExtension”将被您的扩展名所独有的东西(例如其名称)代替。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM