diff --git a/app/src/main/kotlin/com/chmouel/liseur/reader/PageSwaps.kt b/app/src/main/kotlin/com/chmouel/liseur/reader/PageSwaps.kt index 58ada0b6..9ee2789e 100644 --- a/app/src/main/kotlin/com/chmouel/liseur/reader/PageSwaps.kt +++ b/app/src/main/kotlin/com/chmouel/liseur/reader/PageSwaps.kt @@ -340,10 +340,10 @@ internal object PageSwaps { selectionIntersectsAppliedSwap(url, s) { if (!here(url)) return null; const selection = window.getSelection(); - if (!selection || selection.rangeCount === 0 || selection.isCollapsed) return false; + if (!selection || selection.rangeCount === 0 || selection.isCollapsed) return null; let at = null; try { at = locate(s.selector, s.before, s.text); } catch (e) {} - if (!at) return false; + if (!at) return null; const bounds = new Map(); for (let i = at.start; i <= at.end; i++) { const node = at.nodes[i]; diff --git a/app/src/main/kotlin/com/chmouel/liseur/reader/TranslatedPages.kt b/app/src/main/kotlin/com/chmouel/liseur/reader/TranslatedPages.kt index 6046746b..c54d2f66 100644 --- a/app/src/main/kotlin/com/chmouel/liseur/reader/TranslatedPages.kt +++ b/app/src/main/kotlin/com/chmouel/liseur/reader/TranslatedPages.kt @@ -69,6 +69,8 @@ internal object TranslatedPages { /** The selected words as the translated page currently shows them. */ @OptIn(ExperimentalReadiumApi::class) suspend fun selectedText(nav: EpubNavigatorFragment): String? { + val web = visibleWebView(nav.publicationView) ?: return null + val url = web.url ?: return null val result = try { withTimeoutOrNull(ANSWER_MS) { nav.evaluateJavascript(SELECTION_TEXT_SCRIPT) @@ -78,7 +80,7 @@ internal object TranslatedPages { } catch (_: Exception) { null } ?: return null - return decodeSelectionText(result) + return decodeSelectionText(result)?.takeIf { stillShows(nav, web, url) } } @OptIn(ExperimentalReadiumApi::class) diff --git a/app/src/test/kotlin/com/chmouel/liseur/reader/PageSwapsTest.kt b/app/src/test/kotlin/com/chmouel/liseur/reader/PageSwapsTest.kt index 9545862c..9566a51e 100644 --- a/app/src/test/kotlin/com/chmouel/liseur/reader/PageSwapsTest.kt +++ b/app/src/test/kotlin/com/chmouel/liseur/reader/PageSwapsTest.kt @@ -78,6 +78,12 @@ class PageSwapsTest { assertTrue(js.contains("edits.get(node)")) assertTrue(js.contains("range.compareBoundaryPoints(Range.END_TO_START, changed)")) assertTrue(js.contains("range.compareBoundaryPoints(Range.START_TO_END, changed)")) + val body = js.substringAfter("selectionIntersectsAppliedSwap(url, s) {") + .substringBefore("markedAt(url, s, fx, fy) {") + assertTrue( + body.contains("if (!selection || selection.rangeCount === 0 || selection.isCollapsed) return null;"), + ) + assertTrue(body.contains("if (!at) return null;")) } @Test