1

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?

Pida
  • 928
  • 9
  • 32
  • What do you mean 'without changing vendor code'? – TKoL Aug 11 '20 at 10:55
  • Vendor code is code that was written by someone else. Typically, web frameworks place it in a `vendor` directory (though in my case, the file in question is then copied to the public directory because it's frontend JavaScript code). I'm looking for a solution that leaves this code untouched. – Pida Aug 11 '20 at 11:23

0 Answers0