import * as fs from 'fs'; import { promisify } from 'util'; import { Chromeless } from 'chromeless'; import * as cheerio from 'cheerio'; declare module 'util' { export function promisify( func: (data: any, cb: (err: NodeJS.ErrnoException, data?: T) => void, ) => void): (...input: any[]) => Promise; } export interface IUrl { url: string; dist_file: string; } const writeFileAsync = promisify(fs.writeFile); async function run(url: IUrl) { const chromeless = new Chromeless() let html = await chromeless .goto(url.url) .wait(5000) .html(); let $ = cheerio.load(html); let keepgoing = true; while (keepgoing) { const t = $('[data-remove]').first().toString(); if (t) { html = html.replace(t, ''); $ = cheerio.load(html); } keepgoing = !!t; } writeFileAsync(url.dist_file, html).then((err) => { if (err) throw err; console.log(`${url.dist_file} generated!`) }).catch(console.error.bind(console)); await chromeless.end() } export function generateSite(urls: Array) { urls.forEach((url: IUrl) => { run(url).catch(console.error.bind(console)) }) }