diff options
author | Andrew Dolgov <[email protected]> | 2024-01-14 19:25:16 +0300 |
---|---|---|
committer | Andrew Dolgov <[email protected]> | 2024-01-14 19:25:16 +0300 |
commit | 477ee209397ef9e10d41dae26ad24997b9cb1c92 (patch) | |
tree | 2f029f9197a1af5f99063e78ef277b9adae4600c /js/reader.js | |
parent | d5cb80de13cf5fdb98895a1b983994c24e59048e (diff) |
properly deal with situation when there's neither local nor remote location stored for book
Diffstat (limited to 'js/reader.js')
-rw-r--r-- | js/reader.js | 33 |
1 files changed, 23 insertions, 10 deletions
diff --git a/js/reader.js b/js/reader.js index 26a090d..aeae46c 100644 --- a/js/reader.js +++ b/js/reader.js @@ -265,8 +265,6 @@ const Reader = { // legacy format is array of objects {cfi: ..., page: ...} if (locations && typeof locations[0] == "string") { console.log('loading local locations...'); - - try { await book.locations.load(locations); return; @@ -1319,34 +1317,49 @@ const Reader = { } }, openLastRead: async function(rendition) { + let location_found = false; + const lr_local = await localforage.getItem(Reader.cacheId("lastread")) || {}; if (lr_local && lr_local.cfi) { console.log('using local lastread cfi', lr_local.cfi); try { await rendition.display(lr_local.cfi); + + location_found = true; } catch (e) { console.warn(e); } } if (App.isOnline()) { - const lr_remote = await $.post("backend.php", { op: "getlastread", id: $.urlParam("id") }); + try { + const lr_remote = await $.post("backend.php", { op: "getlastread", id: $.urlParam("id") }); - console.log('got remote lastread', lr_remote, lr_local); + console.log('got remote lastread', lr_remote, lr_local); - if (lr_remote) { - if (lr_remote.cfi && lr_local.cfi != lr_remote.cfi && (!lr_local.timestamp || lr_remote.timestamp <= lr_local.timestamp)) { - console.log('using remote lastread (timestamp is newer or local timestamp is missing)'); + if (lr_remote) { + if (lr_remote.cfi && lr_local.cfi != lr_remote.cfi && (!lr_local.timestamp || lr_remote.timestamp <= lr_local.timestamp)) { + console.log('using remote lastread (timestamp is newer or local timestamp is missing)'); - await localforage.setItem(Reader.cacheId("lastread"), - {cfi: lr_remote.cfi, page: lr_remote.page, total: lr_remote.total, timestamp: lr_remote.timestamp}); + await localforage.setItem(Reader.cacheId("lastread"), + {cfi: lr_remote.cfi, page: lr_remote.page, total: lr_remote.total, timestamp: lr_remote.timestamp}); - await rendition.display(lr_remote.cfi); + await rendition.display(lr_remote.cfi); + + location_found = true; + } } + } catch (e) { + console.log('unable to get remote lastread, continuing...'); + console.warn(e); } } + // fallback if there's no local nor remote location, open first page + if (!location_found) + rendition.display(); + $(".loading").hide(); }, }, |