2016年4月11日 星期一

[JavaScript]Get XMLHttpRequest Traffic

 Case: finding network traffic of a website, can't see relevant in the Console->network in Chrome, dig into code, it is XMLHttpRequest and not log in Console->Network

How: In console add hook into XMLHttpRequest

Code:
var rawOpen = XMLHttpRequest.prototype.open;

XMLHttpRequest.prototype.open = function() {
    if (!this._hooked) {
        this._hooked = true;
        setupHook(this);
    }
    rawOpen.apply(this, arguments);
}

function setupHook(xhr) {
    function getter() {
        console.log('get responseText');

        delete xhr.responseText;
        var ret = xhr.responseText;
        setup();
        console.log('url : %s',xhr.responseURL);
        console.log('ret : %s',ret);
        return ret;
    }

    function setter(str) {
        console.log('set responseText: %s', str);
    }

    function setup() {
        Object.defineProperty(xhr, 'responseText', {
            get: getter,
            set: setter,
            configurable: true
        });
    }
    setup();



Source : http://stackoverflow.com/questions/16959359/intercept-xmlhttprequest-and-modify-responsetext

3 則留言:

  1. Nice & Informative Blog !
    Our team at QuickBooks Customer Service Number is ready to answer all your queries and questions through thick and thin.

    回覆刪除
  2. Hey! What a wonderful blog. I loved your blog. QuickBooks is the best accounting software, however, it has lots of bugs like QuickBooks Error. To fix such issues, you can contact experts via QuickBooks Support Phone Number

    回覆刪除