【问题标题】:Firefox addon - how to close tabs after opening them?Firefox 插件 - 打开标签后如何关闭它们?
【发布时间】:2014-08-25 03:31:31
【问题描述】:

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

我一直在查看 MDN 参考资料,但我无法清楚地了解对象模型。

插件会在标签页中打开多个 URL。
DOMContentLoaded 事件将“选项卡”推送到数组中。
稍后我处理数组中的每个“选项卡”并用它的 DOM 做一些事情。
然后我想关闭标签。

在下面的代码 sn-ps 中,除了关闭标签之外,它似乎都可以工作。

问题:

  • 在DOMloadedFunction中,“event.target”到底是什么类型的对象?
  • 在 DOMloadedFunction 中,如何为每个函数保留某种“句柄” 新标签,以便我稍后关闭?

代码sn-ps:

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

任何建议表示赞赏,谢谢。

【问题讨论】:

    标签: javascript firefox tabs firefox-addon


    【解决方案1】:

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

    【讨论】:

    • 非常感谢这个 Noitidart。现在我要真正展示我还需要学习多少东西!......我让它工作了,但必须用下划线作为前缀:......(DOMWin.gBrowser._getTabForContentWindow(......我在你的另一个在这里发帖:(stackoverflow.com/questions/24421077/…)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多