Skip to content

Instantly share code, notes, and snippets.

@mpfund
Created August 9, 2016 06:31
Show Gist options
  • Save mpfund/0d11c842e55d3b224c060913f1190b7f to your computer and use it in GitHub Desktop.
Save mpfund/0d11c842e55d3b224c060913f1190b7f to your computer and use it in GitHub Desktop.
modify firefox response (addon)
/////// START - DO NOT EDIT
var {classes: Cc, interfaces: Ci, results: Cr, Constructor: CC, utils: Cu} = Components;
Cu.import('resource://gre/modules/Services.jsm');
var BinaryInputStream = CC('@mozilla.org/binaryinputstream;1', 'nsIBinaryInputStream', 'setInputStream');
var BinaryOutputStream = CC('@mozilla.org/binaryoutputstream;1', 'nsIBinaryOutputStream', 'setOutputStream');
var StorageStream = CC('@mozilla.org/storagestream;1', 'nsIStorageStream', 'init');
function TracingListener() {
this.receivedChunks = []; // array for incoming data. holds chunks as they come, onStopRequest we join these junks to get the full source
this.responseBody; // we'll set this to the
this.responseStatusCode;
this.deferredDone = {
promise: null,
resolve: null,
reject: null
};
this.deferredDone.promise = new Promise(function(resolve, reject) {
this.resolve = resolve;
this.reject = reject;
}.bind(this.deferredDone));
Object.freeze(this.deferredDone);
this.promiseDone = this.deferredDone.promise;
}
TracingListener.prototype = {
onDataAvailable: function(aRequest, aContext, aInputStream, aOffset, aCount) {
var iStream = new BinaryInputStream(aInputStream) // binaryaInputStream
//var sStream = new StorageStream(8192, aCount, null); // storageStream // not sure why its 8192 but thats how eveyrone is doing it, we should ask why
//var oStream = new BinaryOutputStream(sStream.getOutputStream(0)); // binaryOutputStream
// Copy received data as they come.
var data = iStream.readBytes(aCount);
this.receivedChunks.push(data);
//oStream.writeBytes(data, aCount);
//this.originalListener.onDataAvailable(aRequest, aContext, sStream.newInputStream(0), aOffset, aCount);
},
onStartRequest: function(aRequest, aContext) {
this.originalListener.onStartRequest(aRequest, aContext);
},
onStopRequest: function(aRequest, aContext, aStatusCode) {
this.responseBody = this.receivedChunks.join("");
this.responseBody = this.responseBody.replace(/Yelp/g,'Melp');
var sStream = new StorageStream(8192, this.responseBody.length, null);
var oStream = new BinaryOutputStream(sStream.getOutputStream(0));
oStream.writeBytes(this.responseBody, this.responseBody.length);
this.originalListener.onDataAvailable(aRequest, aContext, sStream.newInputStream(0), 0, this.responseBody.length);
delete this.receivedChunks;
this.responseStatus = aStatusCode;
this.originalListener.onStopRequest(aRequest, aContext, aStatusCode);
this.deferredDone.resolve();
},
QueryInterface: function(aIID) {
if (aIID.equals(Ci.nsIStreamListener) || aIID.equals(Ci.nsISupports)) {
return this;
}
throw Cr.NS_NOINTERFACE;
}
};
var httpResponseObserver = {
observe: function(aSubject, aTopic, aData) {
var newListener = new TracingListener();
aSubject.QueryInterface(Ci.nsITraceableChannel);
newListener.originalListener = aSubject.setNewListener(newListener);
/////// END - DO NOT EDIT
newListener.promiseDone.then(
function() {
// no error happened
console.log('yay response done:', newListener.responseBody);
},
function(aReason) {
// promise was rejected, right now i didnt set up rejection, but i should listen to on abort or bade status code then reject maybe
}
).catch(
function(aCatch) {
console.error('something went wrong, a typo by dev probably:', aCatch);
}
);
}
};
Services.obs.addObserver(httpResponseObserver, 'http-on-examine-response', false);
// Services.obs.removeObserver(httpResponseObserver, 'http-on-examine-response'); // call this when you dont want to listen anymore
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment