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 \"
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).