mirror of
https://github.com/NohamR/pdfy.git
synced 2026-10-10 18:29:55 +00:00
update
This commit is contained in:
@@ -4,6 +4,7 @@ import path from 'node:path'
|
|||||||
import readline from 'node:readline/promises'
|
import readline from 'node:readline/promises'
|
||||||
import { parseArgs } from './cli.js'
|
import { parseArgs } from './cli.js'
|
||||||
import { loadConfigFile, loadPreferences, readCustomCss } from './config.js'
|
import { loadConfigFile, loadPreferences, readCustomCss } from './config.js'
|
||||||
|
import { getOutputFilename } from './pdf.js'
|
||||||
import { convert, BotProtectionError } from './pdfy.js'
|
import { convert, BotProtectionError } from './pdfy.js'
|
||||||
import logger from './logger.js'
|
import logger from './logger.js'
|
||||||
|
|
||||||
@@ -20,7 +21,7 @@ async function promptForInput () {
|
|||||||
async function main () {
|
async function main () {
|
||||||
const { url, opts } = parseArgs()
|
const { url, opts } = parseArgs()
|
||||||
|
|
||||||
await loadConfigFile(opts.config)
|
const config = await loadConfigFile(opts.config)
|
||||||
|
|
||||||
const prefsPath = opts.prefs || null
|
const prefsPath = opts.prefs || null
|
||||||
const cssPath = opts.css || null
|
const cssPath = opts.css || null
|
||||||
@@ -44,9 +45,9 @@ async function main () {
|
|||||||
|
|
||||||
const sizeKb = (result.pdfBuffer.length / 1024).toFixed(0)
|
const sizeKb = (result.pdfBuffer.length / 1024).toFixed(0)
|
||||||
logger.info(`PDF generated: "${result.title}" (${sizeKb} KB)`)
|
logger.info(`PDF generated: "${result.title}" (${sizeKb} KB)`)
|
||||||
if (result.outputPath) {
|
const savePath = result.outputPath || getOutputFilename(result.title)
|
||||||
logger.info(`Saved to: "${result.outputPath}"`)
|
fs.writeFileSync(savePath, result.pdfBuffer)
|
||||||
}
|
logger.info(`Saved to: "${savePath}"`)
|
||||||
return
|
return
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
if (err instanceof BotProtectionError && attempt === 0) {
|
if (err instanceof BotProtectionError && attempt === 0) {
|
||||||
|
|||||||
@@ -41,11 +41,11 @@ export function enhanceHtml (html, customCss) {
|
|||||||
return enhanced
|
return enhanced
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function generatePdf (page, html, outputPath) {
|
export async function generatePdf (page, html, outputPath, { timeout = 30000 } = {}) {
|
||||||
await page.setContent(html, { waitUntil: 'networkidle0' })
|
await page.setContent(html, { waitUntil: 'networkidle0', timeout })
|
||||||
await page.waitForFunction(
|
await page.waitForFunction(
|
||||||
() => Array.from(document.images).every(img => img.complete),
|
() => Array.from(document.images).every(img => img.complete),
|
||||||
{ timeout: 15000, polling: 200 }
|
{ timeout, polling: 200 }
|
||||||
).catch(() => logger.debug('Image wait timed out, continuing...'))
|
).catch(() => logger.debug('Image wait timed out, continuing...'))
|
||||||
const pdfOptions = {
|
const pdfOptions = {
|
||||||
format: 'A4',
|
format: 'A4',
|
||||||
|
|||||||
@@ -19,6 +19,7 @@ import { VALID_THEMES } from './cli.js'
|
|||||||
import logger from './logger.js'
|
import logger from './logger.js'
|
||||||
|
|
||||||
const EXTENSION_BASE = 'chrome-extension://'
|
const EXTENSION_BASE = 'chrome-extension://'
|
||||||
|
const NAV_TIMEOUT = 30000
|
||||||
|
|
||||||
export { launchBrowser, VALID_THEMES }
|
export { launchBrowser, VALID_THEMES }
|
||||||
|
|
||||||
@@ -45,6 +46,8 @@ export async function convert (url, options = {}) {
|
|||||||
|
|
||||||
logger.info(`Theme: ${theme}`)
|
logger.info(`Theme: ${theme}`)
|
||||||
|
|
||||||
|
const timeout = NAV_TIMEOUT
|
||||||
|
|
||||||
await checkExtension()
|
await checkExtension()
|
||||||
|
|
||||||
let browser = externalBrowser
|
let browser = externalBrowser
|
||||||
@@ -62,7 +65,7 @@ export async function convert (url, options = {}) {
|
|||||||
if (html) {
|
if (html) {
|
||||||
await articlePage.setContent(html, { waitUntil: 'networkidle0' })
|
await articlePage.setContent(html, { waitUntil: 'networkidle0' })
|
||||||
} else {
|
} else {
|
||||||
await articlePage.goto(url, { waitUntil: 'networkidle0', timeout: 60000 })
|
await articlePage.goto(url, { waitUntil: 'networkidle0', timeout })
|
||||||
}
|
}
|
||||||
const article = await extractArticle(articlePage, getReadabilityPath())
|
const article = await extractArticle(articlePage, getReadabilityPath())
|
||||||
await articlePage.close()
|
await articlePage.close()
|
||||||
@@ -95,7 +98,7 @@ export async function convert (url, options = {}) {
|
|||||||
logger.debug(`Opening Reader View: ${readerUrl}`)
|
logger.debug(`Opening Reader View: ${readerUrl}`)
|
||||||
|
|
||||||
const readerPage = await browser.newPage()
|
const readerPage = await browser.newPage()
|
||||||
await readerPage.goto(readerUrl, { waitUntil: 'load', timeout: 60000 })
|
await readerPage.goto(readerUrl, { waitUntil: 'load', timeout })
|
||||||
await waitForReaderView(readerPage)
|
await waitForReaderView(readerPage)
|
||||||
await new Promise((resolve) => setTimeout(resolve, 1000))
|
await new Promise((resolve) => setTimeout(resolve, 1000))
|
||||||
|
|
||||||
@@ -120,7 +123,7 @@ export async function convert (url, options = {}) {
|
|||||||
fs.mkdirSync(path.dirname(outputPath), { recursive: true })
|
fs.mkdirSync(path.dirname(outputPath), { recursive: true })
|
||||||
}
|
}
|
||||||
|
|
||||||
const pdfBuffer = await generatePdf(pdfPage, enhancedHtml, outputPath)
|
const pdfBuffer = await generatePdf(pdfPage, enhancedHtml, outputPath, { timeout })
|
||||||
await pdfPage.close()
|
await pdfPage.close()
|
||||||
|
|
||||||
return { title, pdfBuffer, outputPath }
|
return { title, pdfBuffer, outputPath }
|
||||||
|
|||||||
Reference in New Issue
Block a user