fix: replace CDN-loaded Readability with server-side @mozilla/readability#68
Open
octo-patch wants to merge 1 commit into
Open
fix: replace CDN-loaded Readability with server-side @mozilla/readability#68octo-patch wants to merge 1 commit into
octo-patch wants to merge 1 commit into
Conversation
…lity (fixes mishushakov#8) The text format mode previously loaded @mozilla/readability from the skypack CDN via page.evaluate(), which failed on sites with strict CSP (e.g. Hacker News), because the CDN URL was blocked by the site's script-src policy. This commit replaces the in-browser CDN import with a server-side approach: the page HTML is fetched via page.content() and parsed using @mozilla/readability and jsdom running in Node.js, where CSP does not apply. Falls back to page.innerText('body') if Readability cannot parse the page.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #8
Problem
The
textformat mode loaded@mozilla/readabilityfrom the skypack CDN viapage.evaluate(), which runs in the browser context. Sites with strict Content Security Policy (CSP) — like Hacker News — block external CDN URLs in theirscript-srcdirective, causing the import to fail silently or throw an error.Solution
Replace the in-browser CDN import with a server-side approach:
page.content()(runs in Node.js, not the browser)@mozilla/readability+jsdomon the server side, where CSP restrictions do not applypage.innerText('body')if Readability cannot extract an articleThis moves the dependency from a runtime CDN fetch to a proper npm package, making it reliable across all sites regardless of their CSP.
Changes
src/preprocess.ts: Replacepage.evaluate()CDN import with server-sideReadability+JSDOMpackage.json: Add@mozilla/readabilityandjsdomas dependencies; add@types/jsdomas devDependencyTesting
The fix can be verified by running the scraper in
textmode againsthttps://news.ycombinator.com, which previously failed due to CSP blocking the skypack CDN.