Last active
October 4, 2017 02:58
-
-
Save Noitidart/d0b08629ee2804538ad9 to your computer and use it in GitHub Desktop.
_ff-addon-snippet-copyOfRequestSource - This is a copy paste example of how to get a copy of the source code per request.
This file contains hidden or 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
/////// 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); // 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(''); | |
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
README
Rev1
TraceableListener
, you should only add code in place ofconsole.log('yay response done:', newListener.responseBody);
Services.obs.addObserver(httpResponseObserver, 'http-on-examine-response', false);
and the removal of itRev2
Rev3
Rev4
Rev5
Rev6
According to the interface spec nsIStorageStream.init doesn't have a third argument: https://dxr.mozilla.org/mozilla-central/source/xpcom/io/nsIStorageStream.idl#31 - Thanks @freaktechnik
Thanks @tew for the catch