简体   繁体   English

Firefox插件-打开选项卡后如何关闭它们?

[英]Firefox addon - how to close tabs after opening them?

In a javascript Firefox addon, please how can I keep an array of pointers to tabs the addon opens? 在javascript Firefox插件中,如何保持指向插件打开的标签的指针数组?
Ultimately I want to be able to process the DOM on each tab and then close the tab afterwards. 最终,我希望能够处理每个选项卡上的DOM,然后再关闭该选项卡。

I've been looking at the MDN reference, but I can't get a clear picture in my head of the object model. 我一直在看MDN参考,但是我在对象模型的脑海中看不清图片。

The addon opens a number of URLs in tabs. 该插件在选项卡中打开许多URL。
The DOMContentLoaded event pushes the "tabs" into an array. DOMContentLoaded事件将“标签”推入数组。
Later I process each "tab" in the array and do some stuff with it's DOM. 稍后,我处理数组中的每个“选项卡”,并对其DOM做一些处理。
Then I want to close the tab. 然后我要关闭选项卡。

In the code snippets below, it all seems to work except closing the tab. 在下面的代码段中,除了关闭选项卡之外,其他所有功能似乎都可以使用。

Questions: 问题:

  • In DOMloadedFunction, what type of object exactly is "event.target" ? 在DOMloadedFunction中,“ event.target”到底是什么类型的对象?
  • In DOMloadedFunction, how can I keep some kind of "handle" to each new tab so I can close it later? 在DOMloadedFunction中,如何为每个新选项卡保留某种“句柄”,以便以后可以关闭它?

Code snippets: 代码段:

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

Any suggestions appreciated, Thanks. 任何建议表示赞赏,谢谢。

Try this out, i didnt test it. 试试看,我没有测试。 I also use getWeakReference so if your document is unloaded, it won't stay open in memory. 我也使用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
}

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

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