Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle epub file "downloads" from the ZIM in jQuery mode #439

Closed
Jaifroid opened this issue Nov 18, 2018 · 5 comments
Closed

Handle epub file "downloads" from the ZIM in jQuery mode #439

Jaifroid opened this issue Nov 18, 2018 · 5 comments
Assignees

Comments

@Jaifroid
Copy link
Member

This would support "downloading" (extracting) the epub version of book files stored in recent Project Gutenberg ZIMs.

I believe this can be achieved with the download attirbute of the anchor element in html5.

The epubs are linked from the text version of book pages. They are also linked from the proprietary UI that these ZIMs employ, but books can only be accessed from our own search box in jQuery mode currently, due to the fact that the JavaScript that renders the UI contains URLs that are loaded dynamically, and to support this, the loading function inside the script would need to be patched (!), and patching would be pretty non-generic.

Still, the ZIMs are searchable and usable from our own interface, but it is only possible to search by title, currently.

It should be noted that it is quite likely that in Extensions and other contexts, the JS which currently works in SW mode would be blocked. For example, it uses XMLHttpRequests in the script, has some script in <script>…</script> tags, etc.

I wonder if we shouldn't ask for an alternative interface to be provided for non-JS-capable clients.

@Jaifroid Jaifroid self-assigned this Nov 18, 2018
@Jaifroid
Copy link
Member Author

I've added a branch with preliminary support for download links in jQuery mode: Support-epbub-downloads-from-Gutenberg-ZIMs . It does it via BLOBs, which I think is the only way in html5 (see this stackexchange article). I've so far only tested in Edge, but it opens a nice epub reader if you ask to open the file when offered the choice (Edge has built-in support for epubs).

I've tried to make it generic, so that it can be extended for any filetypes.

@mossroy If you have a moment to look at the code, and have any preliminary advice about possible gotchas, or about making it more generic, let me know (or else wait for a PR and do it then). I have to do more testing before PR.

@Jaifroid
Copy link
Member Author

I've made an issue openzim/gutenberg#75 for the poor user experience with these ZIMs if the client cannot execute JavaScript in the ZIM.

@Jaifroid
Copy link
Member Author

Jaifroid commented Nov 20, 2018

OK, this code works in Edge and Chromium, but not currently in any edition of Firefox or IE11. Strange. Maybe the latter browsers don't recognize mimetype application/epub+zim?

UPDATE: I've fixed downloading in Firefox (including FFOS). The clue was provided in a comment to this post: https://stackoverflow.com/a/27280611/9727685 (the anchor has to be added to the DOM for FF to be able to click it). The remaining problem is Internet Explorer, which crashes with an exception when the click code is called. I've tried using jQuery, and it masks the exception, but doesn't actually work in IE and Edge (at least), so that's no good.

FURTHER UPDATE: I've worked around the IE issue, so the code now works in all the browsers we support, I think.

@Jaifroid
Copy link
Member Author

Jaifroid commented Jan 7, 2019

I have rebased the branch Support-epbub-downloads-from-Gutenberg-ZIMs on top of current master.

@Jaifroid
Copy link
Member Author

Jaifroid commented Jan 9, 2019

I created #462 to address this issue. It should be noted that current Gutenberg ZIMs do not appear to make use of the HTML download attribute, and use a custom "fa-download" class instead. However, I don't rely on either (although the code does detect the download attribute if it is set). If we detect a downloadable file type, we have to create an anchor linking to a BLOB containing the object, with a download attribute specifying the filename to use for saving. The anchor has to be attached to the UI, otherwise Firefox cannot programmatically click it (the other browsers can). So I've attached it to a user-dismissible Bootstrap alert box in the footer area. The user can dismiss the box, or else it will be removed when a new article/page is loaded. Screenshot below. I believe the code is now ready for review.

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant