在javascript Firefox插件中,请问如何保存插件打开的标签指针数组? 最终,我希望能够在每个选项卡上处理DOM,然后关闭选项卡。
我一直在看MDN参考,但我无法在对象模型的头脑中得到清晰的图片。
插件会在标签页中打开多个网址 DOMContentLoaded事件将“tabs”推送到数组中 后来我处理数组中的每个“tab”并用它的DOM做一些事情 然后我想关闭标签。
在下面的代码段中,除了关闭标签外,一切似乎都有效。
问题:
代码段:
var arrPages = [];
//Open the tab
newTabBrowser = gBrowser.getBrowserForTab(gBrowser.addTab(inURL));
newTabBrowser.addEventListener("DOMContentLoaded", DOMloadedFunction, true);
// DOMContentLoaded eventhandler
DOMloadedFunction : function (event)
{
// store the tab
arrPages.push(event.target);
}
// Later the array is processed...
oPage = arrPages.shift();
oPage.contentDocument <-- Do some stuff with DOM object.
// Now close the tab
oPage.close() // this is what I want to do, but I can't figure out how
// Unfortunately the index for gBrowser.mTabs[] seems to change, so
// this code usually closes the wrong tab.
var aTab = gBrowser.mTabs[gBrowser.getBrowserIndexForDocument(oPage)]
aTab.remove();
任何建议表示赞赏,谢谢。
答案 0 :(得分:0)
试试这个,我没有测试过。我也使用getWeakReference
所以如果您的文档被卸载,它就不会在内存中保持打开状态。
var arrPages = [];
//Open the tab
newTabBrowser = gBrowser.getBrowserForTab(gBrowser.addTab(inURL));
newTabBrowser.addEventListener("DOMContentLoaded", DOMloadedFunction, true);
// DOMContentLoaded eventhandler
DOMloadedFunction: function(event) {
// store the tab
arrPages.push(Cu.getWeakReference(event.target));
}
// Later the array is processed...
oPage = arrPages.shift();
var contDoc = oPage.get();
if (contDoc) { //test if tab is still open
contDoc < --Do some stuff with DOM object.
}
// Now close the tab
oPage.close() // this is what I want to do, but I can't figure out how
var DOMWin = contDoc.defaultView.QueryInterface(Ci.nsIInterfaceRequestor)
.getInterface(Ci.nsIWebNavigation)
.QueryInterface(Ci.nsIDocShellTreeItem)
.rootTreeItem
.QueryInterface(Ci.nsIInterfaceRequestor)
.getInterface(Ci.nsIDOMWindow);
if (DOMWin.gBrowser) {
if (DOMWin.gBrowser.tabContainer) {
DOMWin.gBrowser.removeTab(DOMWin.gBrowser.getTabForContentWindow(contDoc.defaultView));
} else {
DOMWin.close(); //no tabs probably a popup window of some kind
}
} else {
DOMWin.close(); //close window as its probably a popup window of some kind with no tabs
}