nsIXMLHttpRequest
interface有一个channel
类型为 的成员(只能被扩展访问)nsIChannel
。您可以将自己的回调分配给其notificationCallbacks
属性并实现nsIChannelEventSync
接口以接收重定向事件。这些方面的东西:
Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
var req = new XMLHttpRequest();
req.open('GET', document.location);
var oldNotifications = req.channel.notificationCallbacks;
var oldEventSink = null;
req.channel.notificationCallbacks =
{
QueryInterface: XPCOMUtils.generateQI([
Components.interfaces.nsIInterfaceRequestor,
Components.interfaces.nsIChannelEventSink]),
getInterface: function(iid)
{
// We are only interested in nsIChannelEventSink, return the old callbacks
// for any other interface requests.
if (iid.equals(Ci.nsIChannelEventSink))
{
try {
oldEventSink = oldNotifications.QueryInterface(iid);
} catch(e) {}
return this;
}
if (oldNotifications)
return oldNotifications.QueryInterface(iid);
else
throw Components.results.NS_ERROR_NO_INTERFACE;
},
asyncOnChannelRedirect: function(oldChannel, newChannel, flags, callback)
{
var type = null;
if (flags & Components.interfaces.nsIChannelEventSink.REDIRECT_TEMPORARY)
type = "temporary";
else if (flags & Components.interfaces.nsIChannelEventSink.REDIRECT_PERMANENT)
type = "permanent";
else if (flags & Components.interfaces.nsIChannelEventSink.REDIRECT_INTERNAL)
type = "internal";
Components.utils.reportError("Redirect from " + oldChannel.URI.spec + " " +
"to " + newChannel.URI.spec + " " +
(type ? "(" + type + ")" : ""));
if (oldEventSink)
oldEventSink.asyncOnChannelRedirect(oldChannel, newChannel, flags, callback);
else
callback.onRedirectVerifyCallback(Cr.NS_OK);
}
};
req.send(null);
此代码确保在记录任何对nsIChannelEventSync.asyncOnChannelRedirect
.
供参考:nsIInterfaceRequestor,XPCOMUtils。