繁体   English   中英

在Firefox XUL中,如何获取弹出窗口的“文档”对象?

[英]In Firefox XUL, How can I get the “document” object of a popup window?

单击主浏览器窗口上的链接后,将出现另一个弹出浏览器窗口。 我需要检测弹出浏览器窗口并获取其文档对象。 我的审判是:

wm = Components.classes["@mozilla.org/appshell/window-mediator;1"]
     .getService(Components.interfaces.nsIWindowMediator);
var recentWindow = wm.getMostRecentWindow("navigator:browser");
if (recentWindow) {
    var tabBrowser = recentWindow.getBrowser();
    alert(tabBrowser.contentDocument.body.innerHTML);
}

但是我什么也没得到。

我在想也许窗户还没有打开。 而且,也不需要使用getBrowser()执行recentWindow.gBrowser.contentDocument || recentWindow.document recentWindow.gBrowser.contentDocument || recentWindow.document使用窗口侦听器,您可以捕获所有打开的窗口。

所以这是窗口监听器的东西:

Cu.import('resource://gre/modules/Services.jsm');
var windowListener = {
    onOpenWindow: function (aXULWindow) {
        // Wait for the window to finish loading
        let aDOMWindow = aXULWindow.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIDOMWindowInternal || Ci.nsIDOMWindow);
        aDOMWindow.addEventListener("load", function () {
            aDOMWindow.removeEventListener("load", arguments.callee, false);
            var document = aDOMWindow.gBrowser.contentDocument || aDOMWindow.document;
            aDOMWindow.alert(document.body.innerHTML || 'doc has no body element');
            Services.wm.removeListener(windowListener);
        }, false);
    },
    onCloseWindow: function (aXULWindow) {},
    onWindowTitleChange: function (aXULWindow, aNewTitle) {}
};

然后在您单击链接之前执行此操作: Services.wm.addListener(windowListener);

暂无
暂无

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

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