fix: catch parse error

This commit is contained in:
Han Xiao 2024-04-15 19:27:40 -07:00
parent 3134a59d8f
commit 18373626b2

View File

@ -118,12 +118,21 @@ export class PuppeteerControl extends AsyncService {
preparations.push(page.evaluateOnNewDocument(READABILITY_JS)); preparations.push(page.evaluateOnNewDocument(READABILITY_JS));
preparations.push(page.evaluateOnNewDocument(` preparations.push(page.evaluateOnNewDocument(`
function giveSnapshot() { function giveSnapshot() {
let parsedContent;
try {
// Attempt to parse the cloned document
parsedContent = new Readability(document.cloneNode(true)).parse();
} catch (error) {
// If an error occurs, log it and set parsedContent to undefined
parsedContent = undefined;
}
return { return {
title: document.title, title: document.title,
href: document.location.href, href: document.location.href,
html: document.documentElement.outerHTML, html: document.documentElement.outerHTML,
text: document.body.innerText, text: document.body.innerText,
parsed: new Readability(document.cloneNode(true)).parse(), parsed: parsedContent
}; };
} }
`)); `));