问题
Part of the Firefox extension I'm building gets the ID of 'inner windows' being destroyed, like this -
observer.add('inner-window-destroyed', function (subject, data) {
var innerWindowID = subject.QueryInterface(Ci.nsISupportsPRUint64).data;
console.log('destroyed: '+innerWindowID);
})
This works OK, but I'd like to know if that observer can also get the ID of that inner window's outer window.
I can get an outerWindowID from another channel observer in my code, but I'm not sure how to get it from the above observer when an inner window is being destroyed.
Hope that makes sense...
I've referred to the details on these pages, but can't figure it out -
https://developer.mozilla.org/en/Observer_Notifications#Windows
https://developer.mozilla.org/en/Code_snippets/Windows#Uniquely_identifying_DOM_windows
回答1:
The observer only receives the window ID, accessing the window itself is no longer possible at this point. However, this message is always preceded by a dom-window-destroyed
notification that gets the live window instance (as does the outer-window-destroyed
notification). So you could register your observer for dom-window-destroyed
notification as well and do something along these lines:
var innerWindows = {};
...
observer.add("dom-window-destroyed", function(subject, data)
{
var util = subject.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
.getInterface(Components.interfaces.nsIDOMWindowUtils);
try
{
var innerWindow = util.currentInnerWindowID;
var outerWindow = util.outerWindowID;
innerWindows[innerWindow] = outerWindow;
} catch (e) {} // Ignore NS_ERROR_NOT_AVAILABLE
});
observer.add("inner-window-destroyed", function(subject, data)
{
var innerWindow = subject.QueryInterface(Ci.nsISupportsPRUint64).data;
if (innerWindow in innerWindows)
{
var outerWindow = innerWindows[innerWindow];
delete innerWindows[innerWindow];
console.log("destroyed: " + innerWindow + " (" + outerWindow + ")");
}
});
I didn't try it but this looks like it would work.
来源:https://stackoverflow.com/questions/11345543/get-outerwindowid-from-inner-window-destroyed