[removed] Overriding XMLHttpRequest.open()

前端 未结 4 1878
粉色の甜心
粉色の甜心 2020-12-01 04:29

How would I be able to override the XMLHttpRequest.open() method and then catch and alter it\'s arguments?

I\'ve already tried the proxy method but it d

4条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-01 05:24

    You are not modifying the open method inherited by XMLHttpRequest objects but just adding a method to the XMLHttpRequest constructor which is actually never used.

    I tried this code in facebook and I was able to catch the requests:

    (function() {
        var proxied = window.XMLHttpRequest.prototype.open;
        window.XMLHttpRequest.prototype.open = function() {
            console.log( arguments );
            return proxied.apply(this, [].slice.call(arguments));
        };
    })();
    
    /*
        ["POST", "/ajax/chat/buddy_list.php?__a=1", true]
        ["POST", "/ajax/apps/usage_update.php?__a=1", true]
        ["POST", "/ajax/chat/buddy_list.php?__a=1", true]
        ["POST", "/ajax/canvas_ticker.php?__a=1", true]
        ["POST", "/ajax/canvas_ticker.php?__a=1", true]
        ["POST", "/ajax/chat/buddy_list.php?__a=1", true]
    */
    

    So yeah the open method needs to be added to XMLHttpRequest prototype (window.XMLHttpRequest.prototype) not XMLHttpRequest constructor (window.XMLHttpRequest)

提交回复
热议问题