Files
stash/extension/extract.js

32 lines
1.2 KiB
JavaScript
Raw Normal View History

// Runs in the page as a content script, after lib/Readability.js has already
// been injected (see background.js's executeScript files array — order matters).
// Its return value becomes the result of chrome.scripting.executeScript.
(() => {
try {
const docClone = document.cloneNode(true);
const article = new Readability(docClone).parse();
if (article && article.textContent && article.textContent.trim().length > 200) {
return {
url: location.href,
title: article.title || document.title,
content_html: article.content,
content_text: article.textContent,
author: article.byline || undefined,
published_at: article.publishedTime || undefined,
};
}
} catch (e) {
// fall through to raw capture below
}
// Fallback: Readability either failed or returned too little to be useful
// (common on heavily JS-rendered pages) — capture what's actually rendered
// rather than failing outright. Worse capture beats no capture.
return {
url: location.href,
title: document.title,
content_html: document.body ? document.body.innerHTML : '',
content_text: document.body ? document.body.innerText : '',
};
})();