如何区分 Firefox 标签页?
我目前正在开发一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
http-on-modify-request
为您提供实际的请求对象。从那里您需要获取关联的窗口,方法是 nsILoadContext 此处。我使用这段代码:这是一个内容窗口(甚至可能是一个框架)。所以你必须找到相应的选项卡 - 使用 gBrowser.getBrowserForDocument() 。像这样:
现在您有了请求所属的
元素 - 如果有的话。因为请求也可能来自 Firefox UI 或其他浏览器窗口。您可以在该元素上设置自己的 Expando 属性来获取选项卡标识符(选择唯一的名称以避免与其他扩展发生冲突)。像这样:这里的 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:That's a content window (and probably even a frame). So you have to find the corresponding tab - use gBrowser.getBrowserForDocument(). Like this:
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: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).