How can I modify the XMLHttpRequest responsetext received by another function?

前端 未结 7 1930
面向向阳花
面向向阳花 2020-12-01 04:11

I am trying to modify the responseText received by a function that I cannot modify. This function creates a XMLHttpRequest that I can attach to, but I have been unable to \"

相关标签:
7条回答
  • 2020-12-01 04:44

    First-class function variables are wonderful things! function f() {a; b; c; } is exactly the same thing as var f = function () {a; b; c; } This means you can redefine functions as needed. You want to wrap the function Mj to return a modified object? No problem. The fact that the responseText field is read-only is a pain, but if that's the only field you need...

    var Mj_backup = Mj; // Keep a copy of it, unless you want to re-implement it (which you could do)
    Mj = function (a, b, c, d, e) { // To wrap the old Mj function, we need its args
        var retval = Mj_backup(a,b,c,d,e); // Call the original function, and store its ret value
        var retmod; // This is the value you'll actually return. Not a true XHR, just mimics one
        retmod.responseText = retval.responseText; // Repeat for any other required properties
        return retmod;
    }
    

    Now, when your page code calls Mj(), it will invoke your wrapper instead (which will still call the original Mj internally, of course).

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