Skip to content

Instantly share code, notes, and snippets.

@nasirkhan
Forked from fchasen/request_basicauth.js
Last active March 2, 2018 11:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nasirkhan/b0054e92f7e6bb8764189fb50618d4ca to your computer and use it in GitHub Desktop.
Save nasirkhan/b0054e92f7e6bb8764189fb50618d4ca to your computer and use it in GitHub Desktop.
Loading this script after epub.js and it should overwrite the EPUBJS.core.request method.Change the login credentials in the file to your servers.USERNAME = 'abc'PASSWORD = 'xyz'
USERNAME = 'abc'
PASSWORD = 'xyz'
EPUBJS.core.request = function(url, type) {
var supportsURL = window.URL;
var BLOB_RESPONSE = supportsURL ? "blob" : "arraybuffer";
var deferred = new RSVP.defer();
var xhr = new XMLHttpRequest();
//-- Check from PDF.js:
// https://github.com/mozilla/pdf.js/blob/master/web/compatibility.js
var xhrPrototype = XMLHttpRequest.prototype;
if (!('overrideMimeType' in xhrPrototype)) {
// IE10 might have response, but not overrideMimeType
Object.defineProperty(xhrPrototype, 'overrideMimeType', {
value: function xmlHttpRequestOverrideMimeType(mimeType) {}
});
}
xhr.open("GET", url, true, USERNAME, PASSWORD);
xhr.onreadystatechange = handler;
if(type == 'blob'){
xhr.responseType = BLOB_RESPONSE;
}
if(type == "json") {
xhr.setRequestHeader("Accept", "application/json");
}
if(type == 'xml') {
xhr.overrideMimeType('text/xml');
}
xhr.send();
function handler() {
if (this.readyState === this.DONE) {
if (this.status === 200 || this.responseXML ) { //-- Firefox is reporting 0 for blob urls
var r;
if(type == 'xml'){
r = this.responseXML;
}else
if(type == 'json'){
r = JSON.parse(this.response);
}else
if(type == 'blob'){
if(supportsURL) {
r = this.response;
} else {
//-- Safari doesn't support responseType blob, so create a blob from arraybuffer
r = new Blob([this.response]);
}
}else{
r = this.response;
}
deferred.resolve(r);
} else {
deferred.reject(this);
}
}
}
return deferred.promise;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment