[英]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
而且,也不需要使用getBrowser()
执行recentWindow.gBrowser.contentDocument || recentWindow.document
recentWindow.gBrowser.contentDocument || recentWindow.document
Use window listener and you can catch all the opening windows. recentWindow.gBrowser.contentDocument || recentWindow.document
使用窗口侦听器,您可以捕获所有打开的窗口。
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);
然后在您单击链接之前执行此操作: Services.wm.addListener(windowListener);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.