diff --git a/src/sidebar.js b/src/sidebar.js index def1284..9809df8 100644 --- a/src/sidebar.js +++ b/src/sidebar.js @@ -77,7 +77,7 @@ class Book{ console.info(`Downloading book info for ${this.book_id}`); let url = `https://www.safaribooksonline.com/api/v1/book/${this.book_id}/`; return this.downloadJson(url) - .then((book_info)=>{ + .then((book_info) => { this.book_info = book_info; }, onError); } @@ -86,7 +86,7 @@ class Book{ function helper(book, url){ console.info(`Downloading chapter list ${url}`); return book.downloadJson(url) - .then((chapter_list)=>{ + .then((chapter_list) => { book.chapter_list = book.chapter_list.concat(chapter_list.results); @@ -105,21 +105,18 @@ class Book{ downloadMetaContent(){ let downloads = [] - this.insertBookFile({ - url: this.book_info.cover, - file: "cover.img", - context: null, - mime: null - }) + this.insertBookFile( + this.book_info.cover, + "cover.img") downloads.push( this.downloadJson(this.book_info.toc) - .then((json)=>{ + .then((json) => { this.book_toc=json })) downloads.push( this.downloadJson(this.book_info.flat_toc) - .then((json)=>{ + .then((json) => { this.book_flattoc=json })) downloads.push( @@ -135,41 +132,49 @@ class Book{ extractChapterAssets(json){ - if (!json.asset_base_url - || !json.content){ + if (!json.asset_base_url || !json.content){ throw "Missing data." } - // Html - this.insertBookFile({ - url: json.content, - file: json.full_path, - context: null, - mime: null, - content: null, - }) + this.insertBookFile( + json.content, + json.full_path) // List of images. for (let idx in json.images){ - this.insertBookFile({ - url: json.asset_base_url + json.images[idx], - file: json.images[idx], - context: null, - mime: null - }) + this.insertBookFile( + json.asset_base_url + json.images[idx], + json.images[idx]) } // List of stylesheets. for (let idx in json.stylesheets){ - this.insertBookFile({ - url: json.stylesheets[idx].original_url, - file: json.stylesheets[idx].full_path, - context: null, - mime: null - }) + this.insertBookFile( + json.stylesheets[idx].original_url, + json.stylesheets[idx].full_path) } } - insertBookFile(obj){ - this.book_files[obj.url] = obj + downloadContent(){ + return Promise.map(Object.keys(this.book_files), (url) => { + return this.downloadResource(url) + .then((res) => { + if (res.ok){ + console.log(res.headers.get('Content-Type')) + this.book_files[url].headers = res.headers + this.book_files[url].mime = res.headers.get('Content-Type') + this.book_files[url].body = res.blob() + } + }) + },{concurrency: 1}) + } + + insertBookFile(url, filename){ + this.book_files[url] = { + url: url, + filename: filename, + headers: null, + mime: null, + body: null + } } } @@ -182,7 +187,6 @@ function renderInfo(book){ function renderChapterList(book){ // Add chapters to UI for (let chapter_idx in book.chapter_list) { - console.log(book.chapter_list[chapter_idx]) let chapter = book.chapter_list[chapter_idx]; var chapter_dom = $("
") .addClass("list-group-item") @@ -194,15 +198,11 @@ function renderChapterList(book){ } function createEpub(book, epub){ - for (let url in book.raw_book){ - let name = "meta/"+book.raw_book[url].url.replace(/[^a-z0-9]/gi, '_').toLowerCase() - epub.addFile(name, book.raw_book[url].clone().blob()) - } - // epub.addFile("book_info.json", JSON.stringify(book.book_info)) - // epub.addFile("book_files.json", JSON.stringify(book.book_files)) - // epub.addFile("book_toc.json", JSON.stringify(book.book_toc)) - // epub.addFile("book_flattoc.json", JSON.stringify(book.book_flattoc)) epub.addFile("book.json", JSON.stringify(book, null, '\t')) + for (let url in book.book_files){ + file = book.book_files[url] + epub.addFile(file.filename, file.body) + } } function onDownloadBookClicked(){ @@ -218,15 +218,18 @@ function onDownloadBookClicked(){ book = new Book(book_id); book.downloadBookInfo() .then(() => { renderInfo(book); }) - .then(() => { return book.downloadChapterList(); }) - .then(() => { return renderChapterList(book); }) + // .then(() => { return book.downloadChapterList(); }) + // .then(() => { return renderChapterList(book); }) .then(() => { return book.downloadMetaContent(); }) - // .then(() => { return book.downloadContent(); }) + .then(() => { return book.downloadContent(); }) .then(() => { return createEpub(book, epub); }) .then(() => { return epub.generateAsync(); }) .then((file) => { - let filename = "books/" + "test" + ".zip" - // book.filename.replace(/[^a-z0-9]/gi, '_').toLowerCase() + let title = book.book_info.title + let filename = "books/" + + title.replace(/[^a-z0-9]/gi, '_').toLowerCase() + + ".zip" + console.log(`Zip file name ${filename}`) let url = window.URL.createObjectURL(file) browser.downloads.download({ "filename" : filename, url : url}) }) @@ -248,6 +251,6 @@ document.addEventListener('DOMContentLoaded', function() { $('#deselect-all-button').show() $('#download-button').show() -$('#download-section').hide(); + $('#download-section').hide(); // let bookInfo = new BookInfo(); })