Skip to content

Instantly share code, notes, and snippets.

@aburd
Created February 2, 2021 01:38
Show Gist options
  • Save aburd/732921c2945ca70e1c40b1d669f059ff to your computer and use it in GitHub Desktop.
Save aburd/732921c2945ca70e1c40b1d669f059ff to your computer and use it in GitHub Desktop.
import Puppeteer from 'puppeteer';
import { mapSeries } from 'bluebird';
import fs from 'fs';
const url = 'https://cdnjs.com/libraries';
async function getNewPages(browser: Puppeteer.Browser, page: Puppeteer.Page) {
console.log(`Getting libraries...`);
const urls = await page.$$eval('.title a', (els) =>
els.map((el) => (el as HTMLAnchorElement).href),
);
return await mapSeries(urls, async function (url) {
console.log(`Opening ${url}...`);
const page = await browser.newPage();
await page.goto(url);
return page;
});
}
(async function () {
const browser = await Puppeteer.launch();
const page = await browser.newPage();
await page.goto(url);
const pages = await getNewPages(browser, page);
const scriptStrs: string[] = await mapSeries(pages, async (page) => {
console.log(`Getting all scripts from ${page.url()}`);
return await page.evaluate(function () {
const els = [...document.querySelectorAll<HTMLSpanElement>('.asset')];
return els
.map((el) => `<script src="${el.innerText.trim()}"></script>`)
.filter((str) =>
['.scss', '.map', '.d.ts', '.woff', '.tiff'].every(
(ext) => !str.includes(ext),
),
)
.join('\n');
});
});
console.log('Writing script strings...');
const writeStream = fs.createWriteStream('./shell_tests/scripts', {
encoding: 'utf8',
});
writeStream.write(scriptStrs.join('\n'));
writeStream.close();
await browser.close();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment