Created
January 8, 2014 18:31
-
-
Save fchasen/8321848 to your computer and use it in GitHub Desktop.
Loading this script after 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'
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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