Created
December 17, 2015 08:41
-
-
Save the8472/c505e950881dc4d7033b to your computer and use it in GitHub Desktop.
replacing a channel instead of calling `redirect()`
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
const { Cc, Ci, Cr } = require('chrome'); | |
const ioservice = Cc['@mozilla.org/network/io-service;1'].getService(Ci.nsIIOService); | |
const events = require("sdk/system/events"); | |
const listener = function(event) { | |
let {subject, type, data} = event; | |
if (type == 'http-on-modify-request') { | |
let channel = subject.QueryInterface(Ci.nsIHttpChannel); | |
let newUri = ioservice.newURI("http://foo.bar/baz", null, null); | |
let newChan = ioservice.newChannelFromURI(newUri); | |
newChan.loadInfo = channel.loadInfo | |
newChan.loadGroup = channel.loadGroup | |
newChan.notificationCallbacks = channel.notificationCallbacks; | |
var loadCtx = newChan.notificationCallbacks.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsILoadContext); | |
let loadGroup = channel.loadGroup; | |
newChan.loadGroup = loadGroup; | |
//loadGroup.addRequest(newChan, loadCtx) | |
channel.loadGroup = null; | |
newChan.loadFlags |= channel.loadFlags //| Ci.nsIChannel.LOAD_REPLACE; | |
if (channel instanceof Ci.nsIHttpChannelInternal && newChan instanceof Ci.nsIHttpChannelInternal) { | |
newChan.documentURI = channel.documentURI == channel.URI ? newChan.URI : channel.documentURI; | |
} | |
var eventSink = channel.notificationCallbacks.getInterface(Ci.nsIChannelEventSink); | |
eventSink.asyncOnChannelRedirect(channel,newChan,Ci.nsIChannelEventSink.REDIRECT_INTERNAL,function() {}); | |
let replacementListener = { | |
onDataAvailable: function() {}, | |
onStopRequest: function() {}, | |
onStartRequest: function() {} | |
} | |
channel.QueryInterface(Ci.nsITraceableChannel); | |
let oldListener = channel.setNewListener(replacementListener); | |
channel.notificationCallbacks = null; | |
newChan.asyncOpen(oldListener,loadCtx) | |
channel.cancel(Cr.NS_BINDING_REDIRECTED); | |
loadGroup.removeRequest(channel, loadCtx, Cr.NS_BINDING_REDIRECTED) | |
} | |
}; | |
events.on('http-on-modify-request', listener); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment