From 824ebc4d7686e5a3949e2439955140329c2fb904 Mon Sep 17 00:00:00 2001 From: quarz12 Date: Mon, 22 May 2023 16:21:27 +0200 Subject: [PATCH] added event on change position --- cps/static/js/reading/epub-progress.js | 29 ++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/cps/static/js/reading/epub-progress.js b/cps/static/js/reading/epub-progress.js index f53a476e..83cde55b 100644 --- a/cps/static/js/reading/epub-progress.js +++ b/cps/static/js/reading/epub-progress.js @@ -191,9 +191,30 @@ var epubParser; waitFor(reader.book,()=>{ epubParser = new EpubParser(reader.book.archive.zip.files); }) - -window.addEventListener('hashchange',()=>{console.log("test")}) /* -document.getElementById("next").addEventListener('click',calculateProgress); -document.getElementById("prev").addEventListener('click',calculateProgress); +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')); + }); +})(); +window.addEventListener('locationchange',()=>{ + let newPos=epubParser.calculateProgress(); + //getelement set element value +});