Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
transitive-bullshit committed Oct 7, 2024
1 parent 760642a commit 26245da
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ This [example](./examples/B0819W19WD) uses the first page of the scifi book [Rev
</tr>
<tr>
<td>
After doing this for each page, we now have full access to the book's contents and metadata, so we can export it in any format we want. 🎉
After doing this for each page, we now have access to the book's full contents and metadata, so we can export it in any format we want. 🎉
</td>
<td>
Here's a <a href="./examples/B0819W19WD/book-preview.pdf">preview of the PDF output</a> containing only the first page of the book for example purposes.
Expand Down
12 changes: 7 additions & 5 deletions src/extract-kindle-book.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ async function main() {
await fs.mkdir(userDataDir, { recursive: true })
await fs.mkdir(pageScreenshotsDir, { recursive: true })

const krRendererMainImageSelector = '#kr-renderer .kg-full-page-img img'
const bookReaderUrl = `https://read.amazon.com/?asin=${asin}`

const context = await chromium.launchPersistentContext(userDataDir, {
Expand Down Expand Up @@ -262,11 +263,11 @@ async function main() {
const index = pages.length

const src = await page
.locator('#kr-renderer .kg-full-page-img img')
.locator(krRendererMainImageSelector)
.getAttribute('src')

const b = await page
.locator('#kr-renderer .kg-full-page-img img')
.locator(krRendererMainImageSelector)
.screenshot({ type: 'png', scale: 'css' })

const screenshotPath = path.join(
Expand Down Expand Up @@ -294,10 +295,10 @@ async function main() {
break
}

// Occasionally the next page button doesn't work, so ensure that the main
// image src actually changes before continuing.
let retries = 0

// Occasionally the next page button doesn't work, so ensure that the main
// image src actually changes before continuing.
do {
try {
// Navigate to the next page
Expand All @@ -311,6 +312,7 @@ async function main() {
})
}

// Click the next page button
await page
.locator('.kr-chevron-container-right')
.click({ timeout: 1000 })
Expand All @@ -326,7 +328,7 @@ async function main() {
}

const newSrc = await page
.locator('#kr-renderer .kg-full-page-img img')
.locator(krRendererMainImageSelector)
.getAttribute('src')
if (newSrc !== src) {
break
Expand Down

0 comments on commit 26245da

Please sign in to comment.