Altering HTTP Responses in Firefox Extension

后端 未结 3 1666
南旧
南旧 2020-12-09 03:47

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

相关标签:
3条回答
  • 2020-12-09 04:28

    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/

    0 讨论(0)
  • 2020-12-09 04:37

    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(
        function fixenator(details) {
            let filter = browser.webRequest.filterResponseData(details.requestId);
            let decoder = new TextDecoder("utf-8");
            let encoder = new TextEncoder();
            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']
    );
    
    0 讨论(0)
  • 2020-12-09 04:47

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

    0 讨论(0)
提交回复
热议问题