简体   繁体   中英

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

After clicking on a link on the main browser window, another popup browser window appears. I need to detect the popup browser window and get its document object. My trial was:

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

But I didn't get anything.

I'm thinking maybe the window hasn't opened yet. And also no need to use getBrowser() just do recentWindow.gBrowser.contentDocument || recentWindow.document recentWindow.gBrowser.contentDocument || recentWindow.document Use window listener and you can catch all the opening windows.

So this is window listener stuff:

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) {}
};

then right before you click on the link do this: Services.wm.addListener(windowListener);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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