Firefox gBrowser.getBrowserForTab 但没有 gBrowser.getTabForBrowser?
在 Firefox 扩展的 Javascript 中,您可以调用 gBrowser.getBrowserForTab
,但没有 gBrowser.getTabForBrowser
。所以我自己编写了它并且它有效,我只是好奇是否有任何理由我不应该这样做,或者代码是否有任何问题。以下是我的 init
方法,该方法在窗口加载时被调用。
gBrowser.getTabForBrowser = function(browser) {
for (var i=0; i<gBrowser.browsers.length; i++) {
if (gBrowser.getBrowserAtIndex(i) === browser) {
return gBrowser.tabContainer.getItemAtIndex(i);
}
}
return null;
}
(或者应该是gBrowser.prototype.getTabForBrowser = ...
?)
In the Javascript for a Firefox extension, you can call gBrowser.getBrowserForTab
but there is no gBrowser.getTabForBrowser
. So I wrote my own and it works, and I'm just curious if there's any reason I shouldn't be doing this, or if there's anything wrong with the code. The following is in my init
method that gets called when the window loads.
gBrowser.getTabForBrowser = function(browser) {
for (var i=0; i<gBrowser.browsers.length; i++) {
if (gBrowser.getBrowserAtIndex(i) === browser) {
return gBrowser.tabContainer.getItemAtIndex(i);
}
}
return null;
}
(or should it be gBrowser.prototype.getTabForBrowser = ...
?)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
据我所知,没有内置的 getTabForBrowser 函数,所以你必须自己动手。但是,您的代码假定浏览器节点以与选项卡节点相同的 DOM 顺序存储。我不能肯定地说这个假设是否被打破,但考虑到选项卡可以由用户任意重新定位,这不是我所依赖的。
幸运的是,每个选项卡对象都有一个
linkedBrowser
属性。所以你可以像这样重写你的代码:Far as I know there is no built in
getTabForBrowser
function, so you would have to roll your own. However, your code assumes that browser nodes are stored in the same DOM order as tab nodes. I can't say for sure if this assumption is ever broken, but considering tabs can be re-positioned by the user arbitrarily, it is not something I'd rely on.Fortunately, each tab object has a
linkedBrowser
property. So you could rewrite your code like so: