Skip to content Skip to sidebar Skip to footer

Altering Http Responses In Firefox Extension

How can I alter the HTTP response body in a Firefox extension? I have setup an http-on-examine-response observer and an nsIStreamListener object with the code below. After I get th

Solution 1:

You can use nsITraceableChannel to intercept the response.

You should modify the data which is available to what you need and pass it to the innerListener's OnDataAvailable

Below links would help you understand this better.

http://www.softwareishard.com/blog/firebug/nsitraceablechannel-intercept-http-traffic/

http://www.ashita.org/howto-xhr-listening-by-a-firefox-addon/

Solution 2:

For future readers looking for a way to do this in Firefox Quantum, there is an API that lets you filter responses. Using the method for long documents mentioned here, I was able to reliably change what I needed in my (temporary) plugin's background.js like so:

browser.webRequest.onBeforeRequest.addListener(
    functionfixenator(details) {
        let filter = browser.webRequest.filterResponseData(details.requestId);
        let decoder = newTextDecoder("utf-8");
        let encoder = newTextEncoder();
        let str = '';

        filter.ondata = event => {
            str += decoder.decode(event.data, {stream: true});
        };

        filter.onstop = event => {
            str = str.replace(/searchPattern/g, 'replace pattern');
            filter.write(encoder.encode(str));
            filter.close();
        }

        return {};
    },
    {
        urls: ['https://example.com/path/to/url']
        //, types: ['main_frame', 'script', 'sub_frame', 'xmlhttprequest', 'other'] // optional
    }
    , ['blocking']
);

Solution 3:

The observer service just call your listeners. Firefox will receive the requests,call your listeners, and send responses. see Mozilla docs Creating HTTP POSTs.

Post a Comment for "Altering Http Responses In Firefox Extension"