I want to override XMLHttpRequest
's send in order to let my userscript know when data is getting updated on a page through such a request. My override code looks like this:
var oldSend = unsafeWindow.XMLHttpRequest.prototype.send;
unsafeWindow.XMLHttpRequest.prototype.send = function(){
console.log("notified of XHR update");
oldSend.apply(this, arguments);
}
If I inject this into the page (w/o the unsafeWindow
references) it works fine, but I'd like to get this working from userscript scope. unsafeWindow
works for this in Firefox, but not in Chrome. So I grabbed Brock Adams' nifty trick to create a working unsafeWindow
in Chrome:
var bGreasemonkeyServiceDefined = false;
try {
if (typeof Components.interfaces.gmIGreasemonkeyService === "object") {
bGreasemonkeyServiceDefined = true;
}
}
catch (err) {
//Ignore.
}
if ( typeof unsafeWindow === "undefined" || ! bGreasemonkeyServiceDefined) {
unsafeWindow = ( function () {
var dummyElem = document.createElement('p');
dummyElem.setAttribute ('onclick', 'return window;');
return dummyElem.onclick ();
} ) ();
}
However, when I combine the two nothing happens. It all works pasted into the console, but there's neither error nor output when running this from a userscript. Am I doing something wrong or perhaps this is beyond the capabilities of this trick?
Hmm, just tried something simpler, like: unsafeWindow.document.title = 'testing';
and that doesn't work either, so maybe it's not specific to XMLHttpRequest
.
I'm trying to avoid injection into the page if at all possible.