mirror of
https://github.com/futurepress/epub.js.git
synced 2025-10-03 14:59:18 +02:00
Added error handles, sections, updated url resolving, use book.request for requests
This commit is contained in:
parent
c6465177c5
commit
c29e5f84ee
19 changed files with 3049 additions and 683 deletions
|
@ -1,6 +1,6 @@
|
|||
EPUBJS.core = {};
|
||||
|
||||
EPUBJS.core.request = function(url, type, withCredentials) {
|
||||
EPUBJS.core.request = function(url, type, withCredentials, headers) {
|
||||
var supportsURL = window.URL;
|
||||
var BLOB_RESPONSE = supportsURL ? "blob" : "arraybuffer";
|
||||
|
||||
|
@ -12,6 +12,8 @@ EPUBJS.core.request = function(url, type, withCredentials) {
|
|||
// https://github.com/mozilla/pdf.js/blob/master/web/compatibility.js
|
||||
var xhrPrototype = XMLHttpRequest.prototype;
|
||||
|
||||
var header;
|
||||
|
||||
if (!('overrideMimeType' in xhrPrototype)) {
|
||||
// IE10 might have response, but not overrideMimeType
|
||||
Object.defineProperty(xhrPrototype, 'overrideMimeType', {
|
||||
|
@ -21,7 +23,13 @@ EPUBJS.core.request = function(url, type, withCredentials) {
|
|||
if(withCredentials) {
|
||||
xhr.withCredentials = true;
|
||||
}
|
||||
|
||||
xhr.open("GET", url, true);
|
||||
|
||||
for(header in headers) {
|
||||
xhr.setRequestHeader(header, headers[header]);
|
||||
}
|
||||
|
||||
xhr.onreadystatechange = handler;
|
||||
|
||||
if(type == 'blob'){
|
||||
|
@ -65,6 +73,7 @@ EPUBJS.core.request = function(url, type, withCredentials) {
|
|||
deferred.resolve(r);
|
||||
} else {
|
||||
deferred.reject({
|
||||
status: this.status,
|
||||
message : this.response,
|
||||
stack : new Error().stack
|
||||
});
|
||||
|
@ -233,28 +242,52 @@ EPUBJS.core.values = function(object) {
|
|||
};
|
||||
|
||||
EPUBJS.core.resolveUrl = function(base, path) {
|
||||
var url,
|
||||
var url = [],
|
||||
segments = [],
|
||||
// uri = EPUBJS.core.uri(path),
|
||||
folders = base.split("/"),
|
||||
baseUri = EPUBJS.core.uri(base),
|
||||
pathUri = EPUBJS.core.uri(path),
|
||||
baseDirectory = baseUri.directory,
|
||||
pathDirectory = pathUri.directory,
|
||||
// folders = base.split("/"),
|
||||
paths;
|
||||
|
||||
// if(uri.host) {
|
||||
// return path;
|
||||
// }
|
||||
|
||||
folders.pop();
|
||||
|
||||
paths = path.split("/");
|
||||
paths.forEach(function(p){
|
||||
if(p === ".."){
|
||||
folders.pop();
|
||||
}else{
|
||||
segments.push(p);
|
||||
if(baseDirectory[0] === "/") {
|
||||
baseDirectory = baseDirectory.substring(1);
|
||||
}
|
||||
|
||||
if(pathDirectory[pathDirectory.length-1] === "/") {
|
||||
baseDirectory = baseDirectory.substring(0, baseDirectory.length-1);
|
||||
}
|
||||
|
||||
if(pathDirectory[0] === "/") {
|
||||
pathDirectory = pathDirectory.substring(1);
|
||||
}
|
||||
|
||||
if(pathDirectory[pathDirectory.length-1] === "/") {
|
||||
pathDirectory = pathDirectory.substring(0, pathDirectory.length-1);
|
||||
}
|
||||
|
||||
|
||||
directories = baseDirectory.split("/");
|
||||
|
||||
paths = pathDirectory.split("/");
|
||||
|
||||
paths.reverse().forEach(function(part, index){
|
||||
if(part === ".."){
|
||||
directories.pop();
|
||||
} else if(part === directories[directories.length-1]) {
|
||||
directories.pop();
|
||||
segments.unshift(part);
|
||||
} else {
|
||||
segments.push(part);
|
||||
}
|
||||
});
|
||||
|
||||
url = folders.concat(segments);
|
||||
url = url.concat(baseUri.origin, directories, segments, pathUri.filename);
|
||||
|
||||
return url.join("/");
|
||||
};
|
||||
|
@ -267,4 +300,8 @@ EPUBJS.core.documentHeight = function() {
|
|||
document.body.offsetHeight,
|
||||
document.documentElement.offsetHeight
|
||||
);
|
||||
};
|
||||
};
|
||||
|
||||
EPUBJS.core.isNumber = function(n) {
|
||||
return !isNaN(parseFloat(n)) && isFinite(n);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue