Skip to content

Instantly share code, notes, and snippets.

@the8472
Created December 17, 2015 08:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save the8472/c505e950881dc4d7033b to your computer and use it in GitHub Desktop.
Save the8472/c505e950881dc4d7033b to your computer and use it in GitHub Desktop.
replacing a channel instead of calling `redirect()`
"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