Firefox插件-如何关闭标签打开后,他们

Firefox addon - how to close tabs after opening them?

本文关键字:他们 关闭标签 插件 Firefox      更新时间:2023-09-26

在一个javascript Firefox插件,请我如何保持一个指针的选项卡数组的插件打开?
最终,我希望能够处理每个选项卡上的DOM,然后关闭选项卡。

我一直在看MDN参考,但我不能在我的头脑中得到一个清晰的图像的对象模型。

该插件在选项卡中打开了许多url。
DOMContentLoaded事件将"tabs"推入数组。
之后,我处理数组中的每个"tab",并对它的DOM做一些事情。
然后我想关闭选项卡。

在下面的代码片段中,除了关闭选项卡之外,似乎都可以正常工作。

问题:

    在domloaddfunction中,什么类型的对象是"事件"?目标"?
  • 在domloaddfunction中,我如何保持某种"句柄"到每个新选项卡,这样我可以稍后关闭它?

代码片段:

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();

试试这个,我没有测试它。我也使用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
}