I was facing an issue while developing this small userscript. When I wanted to block every XMLHttpRequest
from the running website with my script, nothing was happening (at least with Chrome):
function main() {
// Override XHR.open with a custom function
window.XMLHttpRequest.prototype.open = function() {
// Nothing... so it's supposed to block every xhr.open() call
}
}
main();
Same thing when replacing window
by unsafeWindow
.
However, when I used this little trick, everything worked like a charm:
// No more call to main(), and:
var script = document.createElement("script");
script.textContent = "(" + main.toString() + ")();";
document.body.appendChild(script);
Every call to xhr.open
is replaced by my custom function, no more AJAX.
So I guess the window
element is not the same when main
is called from inside the script than when it's called from a <script></script>
container. Can someone explain me why ?
unsafeWindow
is now supported in Chrome – Sabin