首先,您需要从 a 中DOMWindow
获取nsIChannel
:
function getDOMWindowFromChannel(ch) {
var wp;
try {
if (ch.loadGroup && ch.loadGroup.groupObserver) {
wp = ch.loadGroup.groupObserver.
QueryInterface(Ci.nsIWebProgress);
}
} catch (ex) {}
try {
if (!wp) {
wp = ch.notificationCallbacks.
getInterface(Ci.nsIWebProgress);
}
}
catch (ex) {}
try {
if (wp) {
return wp.DOMWindow || null;
}
}
catch (ex) {}
return null;
}
现在你有了一个DOMWindow
,你需要找到它的顶层窗口DOMWindow
,这不是很直观:
function getToplevelWindow(win) {
try {
return win.QueryInterface(Ci.nsIInterfaceRequestor).
getInterface(Ci.nsIWebNavigation).
QueryInterface(Ci.nsIDocShell).
treeOwner.
QueryInterface(Ci.nsIInterfaceRequestor).
getInterface(Ci.nsIXULWindow).
docShell.
contentViewer.DOMDocument.defaultView;
}
catch (ex) {
// Likely already a top-level window.
return win;
}
}
现在,让我们制作和安装观察者,将所有内容放在一起:
function observe(channel, topic, data) {
if (!(channel instanceof Ci.nsIChannel)) {
return;
}
var win = getDOMWindowFromChannel(channel);
if (!win) {
return;
}
var topWin = getToplevelWindow(win);
if (topWin.location.href.indexOf("chrome://browser/content/hiddenWindow") != 0) {
return;
}
// do stuff, e.g.
console.log(topWin.location.href);
}
Services.obs.addObserver(observe, "http-on-modify-request", false);
应该注意的是,并非所有请求都存在nsIChannel
并且并非所有请求nsIChannel
实际上都具有DOMWindow
或真正loadGroup
关联(例如后台请求),因此所有这些try catch
块。
另外,不要忘记在某个时候再次删除观察者,我跳过了。;)
最后,这是一些实际测试的代码(我在 about:newtab 选项卡上将整个事情作为 Scratchpad 运行,它恰好像附加组件一样具有 chrome 权限):
var hw = Services.appShell.hiddenDOMWindow;
var iframe = hw.document.createElement("iframe");
hw.document.documentElement.appendChild(iframe);
var r = iframe.contentWindow.XMLHttpRequest();
r.open("GET", "http://example.org/");
r.send();