Skip to content

Instantly share code, notes, and snippets.

@anatomic
Last active September 11, 2019 08:20
Show Gist options
  • Save anatomic/3014eed2b748d2f78620c4ddb46ff6a1 to your computer and use it in GitHub Desktop.
Save anatomic/3014eed2b748d2f78620c4ddb46ff6a1 to your computer and use it in GitHub Desktop.
const puppeteer = require("puppeteer");
const readline = require("readline");
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
terminal: false
});
const eq = (a, b) => {
const _a = new URL(a);
const _b = new URL(b);
return _a.host === _b.host;
};
const getUrls = async rl => {
const urls = [];
for await (const url of rl) {
urls.push(url);
}
return urls;
};
const checkRedirect = browser => async url => {
try {
const page = await browser.newPage();
const response = await page.goto(url);
eq(url, response.url()) === false &&
console.log(`${url} redirects to ${response.url()}`);
} catch (e) {
console.error(`Failed to check ${url}`);
}
};
const main = async rli => {
const urls = await getUrls(rli);
console.log(`Checking ${urls.length} URL${urls.length === 1 ? "" : "s"}`);
const browser = await puppeteer.launch({ ignoreHTTPSErrors: true });
await Promise.all(urls.map(checkRedirect(browser)));
browser.close();
};
main(rl).then(() => process.exit(0));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment