Skip to content

fix: support location-based books and handle updated Kindle Cloud Reader UI - #23

Open
emwbae wants to merge 1 commit into
transitive-bullshit:mainfrom
emwbae:fix/location-based-books-and-updated-ui
Open

fix: support location-based books and handle updated Kindle Cloud Reader UI#23
emwbae wants to merge 1 commit into
transitive-bullshit:mainfrom
emwbae:fix/location-based-books-and-updated-ui

Conversation

@emwbae

@emwbae emwbae commented Mar 7, 2026

Copy link
Copy Markdown

Summary

Two bugs that prevent extraction from working on a significant number of Kindle books:

  1. Location-based books produce 0 pagesparsePageNav() in playwright-utils.ts already handles the "Location X of Y" footer format, correctly returning { location, total }. However, the main extraction loop in extract-kindle-book.ts only checks pageNav?.page, which is undefined for location-based books. This triggers the break on the very first iteration, producing an empty result with 0 captured pages.

  2. goToPage() crashes on current Kindle Cloud Reader — Amazon renamed the menu item from "Go to Page" to "Go to Location" and changed the underlying element structure. The hardcoded selectors (ion-item[role="listitem"] with hasText: 'Go to Page', input[placeholder="page number"], ion-button[item-i-d="go-to-modal-go-button"]) all fail to match, causing a timeout that kills the extraction before any pages are captured.

Changes

Location-based book support (main extraction loop)

  • Added const currentPage = pageNav?.page ?? pageNav?.location to fall back to location when page number is undefined
  • Scoped the totalNumContentPages bounds check to only apply when pageNav.page is defined — location-based books don't have a meaningful page-to-content-page mapping, so the original pageNav.page > totalNumContentPages check would never fire anyway (it was the pageNav?.page === undefined check above it that caused the early exit)
  • Updated all downstream references (screenshotPath, pageChunk, assertion messages) to use currentPage instead of pageNav.page

Resilient goToPage() navigation

  • Broadened menu button selector with .or() fallbacks for both ion-button and plain button elements
  • Changed menu item matching from exact 'Go to Page' string to regex /go to (location|page)/i to handle both old and new Kindle Cloud Reader UI
  • Added fallback selectors for the page/location input field (placeholder*="page" and placeholder*="location")
  • Added fallback selectors for the Go button (text match + legacy item-i-d attribute)
  • Wrapped the entire function in try/catch — on failure, dismisses any open modals via Escape keypresses and logs a warning instead of crashing the entire extraction
  • Wrapped the end-of-extraction goToPage(initialPageNav.page) reset in try/catch to prevent a crash after all pages have already been successfully captured

Reproduction

  1. Open any Kindle book that displays "Location X of Y" in the footer (rather than "Page X of Y") — this is common for many self-published and KDP titles
  2. Run npx tsx src/extract-kindle-book.ts
  3. Extraction completes instantly with 0 pages captured, because parsePageNav() returns { location: X, total: Y } (no .page property), and the loop immediately breaks on pageNav?.page === undefined
  4. After this fix, the same book correctly captures all pages via location-based navigation

Related issues

Known limitations

The downstream export scripts (export-book-markdown.ts, export-book-pdf.ts, export-book-audio.ts) use tocItem.page and content.findIndex((c) => c.page >= nextTocItem.page!) to map chapters to content chunks. For location-based books, the PageChunk.page field now contains location numbers rather than page numbers, which may cause chapter boundary mismatches in exports. This is a pre-existing limitation (these scripts couldn't work at all before since extraction produced 0 pages) and would be best addressed in a follow-up PR.

Validated

  • Tested on a location-based book — 181 page screenshots captured successfully
  • goToPage() gracefully recovers when selectors don't match (Escape key dismisses modals)
  • Full pipeline works end-to-end: extraction → transcription (GPT-4.1-mini) → markdown export
  • Passes prettier and eslint (pre-commit hooks ran successfully)

…der UI

parsePageNav() correctly returns { location, total } for books displaying
"Location X of Y", but the main extraction loop only checked pageNav.page,
causing an immediate break on the first iteration — resulting in 0 captured
pages for any location-based book.

Additionally, Amazon's Kindle Cloud Reader UI renamed "Go to Page" to
"Go to Location" and changed several element selectors, causing goToPage()
to timeout and crash the entire extraction process.
@cif49

cif49 commented Apr 7, 2026

Copy link
Copy Markdown

Fyi, as of writing this this branch still works, the main one does not.
Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants