In this question I asked for a way to automatically add headers to XMLHttpRequest
objects. I tried to implement this solution:
(function(open) {
XMLHttpRequest.prototype.open = function(method, url, async, user, pass) {
this.addEventListener("readystatechange", function() {
if (this.readyState == 4)
{
console.log(this.status);
}
}, false);
open.call(this, method, url, async, user, pass);
this.setRequestHeader("Authorization", "Token 123")
};
})(XMLHttpRequest.prototype.open);
This is working working in principle, but not in my case: I want to add a header to some vendor code that is encapsulated using the IIFE pattern:
H5PEditor.FileUploader = (function ($, EventDispatcher) {
// ...
self.upload = function (file, filename) {
var formData = new FormData();
formData.append('file', file, filename);
// ...
var request = new XMLHttpRequest();
// ...
request.open('POST', H5PEditor.getAjaxUrl('files'), true);
request.send(formData);
self.trigger('upload');
};
// ...
})(H5P.jQuery, H5P.EventDispatcher);
If I add the code from the first code sample inside this IIFE, the header is added. But is there a way to accomplish the same without changing vendor code?