The code in epub-progress.js was causing the epub file to be loaded twice over the network, because it created a second instance of the epub.js library. I moved all the progress percentage display code into epub.js. Only the locationchange polyfill code was left in the separate file, renamed to locationchange-polyfill.js. I rewrote the percentage progress code to use epub.js events, giving more succinct and readable code. Also, I added localStorage caching of the epub location calculations, required for displaying percentage progress, which can take up to 60-90 seconds to calculate on longer ebooks. This localStorage caching approach is recommended by epub.js in the `locations.html` example in their repo. Signed-off-by: Ross Williams <ross@ross-williams.net>
22 lines
733 B
JavaScript
22 lines
733 B
JavaScript
// register new event emitter locationchange that fires on urlchange
|
|
// source: https://stackoverflow.com/a/52809105/21941129
|
|
(() => {
|
|
let oldPushState = history.pushState;
|
|
history.pushState = function pushState() {
|
|
let ret = oldPushState.apply(this, arguments);
|
|
window.dispatchEvent(new Event('locationchange'));
|
|
return ret;
|
|
};
|
|
|
|
let oldReplaceState = history.replaceState;
|
|
history.replaceState = function replaceState() {
|
|
let ret = oldReplaceState.apply(this, arguments);
|
|
window.dispatchEvent(new Event('locationchange'));
|
|
return ret;
|
|
};
|
|
|
|
window.addEventListener('popstate', () => {
|
|
window.dispatchEvent(new Event('locationchange'));
|
|
});
|
|
})();
|