This file contains 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
#!/usr/bin/env node | |
// npm i -g @mozilla/readability puppeteer | |
const puppeteer = require("puppeteer"); | |
const fs = require("fs"); | |
const readabilityJsStr = fs.readFileSync( | |
require.resolve("@mozilla/readability/Readability.js"), | |
{ encoding: "utf-8" } | |
); | |
function executor() { | |
return new Readability({}, document).parse(); | |
} | |
const dump = async (url) => { | |
const browser = await puppeteer.launch(); | |
const page = await browser.newPage(); | |
await page.goto(url); | |
const resultArticle = await page.evaluate(` | |
(function(){ | |
${readabilityJsStr} | |
${executor} | |
return executor(); | |
}()) | |
`); | |
const content = ` | |
<!doctype html> | |
<html> | |
<head> | |
<title>${resultArticle.title}</title> | |
</head> | |
<body> | |
${resultArticle.content} | |
</body> | |
</html>`; | |
browser.close(); | |
return content; | |
}; | |
const main = async () => { | |
try { | |
const html = await dump(process.argv[2]); | |
console.log(html); | |
process.exit(0); | |
} catch (e) { | |
console.error(e); | |
process.exit(1); | |
} | |
}; | |
main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment