2023-05-21 20:23:53 +00:00
|
|
|
/**
|
2023-06-05 13:39:09 +00:00
|
|
|
* waits until queue is finished, meaning the book is done loading
|
|
|
|
* @param callback
|
2023-05-21 20:23:53 +00:00
|
|
|
*/
|
2023-06-05 13:39:09 +00:00
|
|
|
function qFinished(callback){
|
|
|
|
let timeout=setInterval(()=>{
|
|
|
|
if(reader.rendition.q.running===undefined)
|
|
|
|
clearInterval(timeout);
|
|
|
|
callback();
|
|
|
|
},300
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-05-21 20:23:53 +00:00
|
|
|
function calculateProgress(){
|
2023-06-05 13:39:09 +00:00
|
|
|
let data=reader.rendition.location.end;
|
|
|
|
return Math.round(epub.locations.percentageFromCfi(data.cfi)*100);
|
2023-05-21 20:23:53 +00:00
|
|
|
}
|
2023-05-24 19:05:50 +00:00
|
|
|
|
|
|
|
// register new event emitter locationchange that fires on urlchange
|
|
|
|
// source: https://stackoverflow.com/a/52809105/21941129
|
2023-05-22 14:21:27 +00:00
|
|
|
(() => {
|
|
|
|
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'));
|
|
|
|
});
|
|
|
|
})();
|
2023-05-24 19:05:50 +00:00
|
|
|
|
2023-05-22 14:21:27 +00:00
|
|
|
window.addEventListener('locationchange',()=>{
|
2023-05-22 21:36:14 +00:00
|
|
|
let newPos=calculateProgress();
|
|
|
|
progressDiv.textContent=newPos+"%";
|
2023-05-22 14:21:27 +00:00
|
|
|
});
|
2023-06-05 13:39:09 +00:00
|
|
|
|
|
|
|
var epub=ePub(calibre.bookUrl)
|
|
|
|
|
|
|
|
let progressDiv=document.getElementById("progress");
|
|
|
|
|
|
|
|
qFinished(()=>{
|
|
|
|
epub.locations.generate().then(()=> {
|
|
|
|
window.dispatchEvent(new Event('locationchange'))
|
|
|
|
});
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|