2022-06-16 04:13:44 -05:00
|
|
|
const puppeteer = require("puppeteer");
|
|
|
|
const { PuppeteerBlocker } = require("@cliqz/adblocker-puppeteer");
|
|
|
|
const fetch = require("cross-fetch");
|
2022-06-17 20:18:48 -05:00
|
|
|
const { screenshotDirectory, pdfDirectory } = require("../config.js");
|
2022-05-26 11:15:07 -05:00
|
|
|
|
|
|
|
module.exports = async (link, id) => {
|
2022-06-17 20:18:48 -05:00
|
|
|
const browser = await puppeteer.launch({
|
2022-07-03 11:49:46 -05:00
|
|
|
args: ["--no-sandbox"],
|
2022-06-17 20:18:48 -05:00
|
|
|
timeout: 10000,
|
|
|
|
});
|
2022-05-26 11:15:07 -05:00
|
|
|
const page = await browser.newPage();
|
|
|
|
|
|
|
|
await PuppeteerBlocker.fromPrebuiltAdsAndTracking(fetch).then((blocker) => {
|
|
|
|
blocker.enableBlockingInPage(page);
|
|
|
|
});
|
|
|
|
|
2022-07-03 11:49:46 -05:00
|
|
|
await page.goto(link, { waitUntil: "load", timeout: 300000 });
|
|
|
|
|
|
|
|
await page.setViewport({
|
|
|
|
width: 1200,
|
|
|
|
height: 800,
|
|
|
|
});
|
|
|
|
|
|
|
|
await autoScroll(page);
|
2022-06-16 04:13:44 -05:00
|
|
|
|
|
|
|
await page.screenshot({
|
2022-06-17 20:18:48 -05:00
|
|
|
path: screenshotDirectory + "/" + id + ".png",
|
2022-06-16 04:13:44 -05:00
|
|
|
fullPage: true,
|
|
|
|
});
|
2022-06-17 20:18:48 -05:00
|
|
|
await page.pdf({ path: pdfDirectory + "/" + id + ".pdf", format: "a4" });
|
2022-05-26 11:15:07 -05:00
|
|
|
|
|
|
|
await browser.close();
|
2022-06-16 04:13:44 -05:00
|
|
|
};
|
2022-07-03 11:49:46 -05:00
|
|
|
|
|
|
|
async function autoScroll(page) {
|
|
|
|
await page.evaluate(async () => {
|
|
|
|
await new Promise((resolve, reject) => {
|
|
|
|
let totalHeight = 0;
|
|
|
|
let distance = 100;
|
|
|
|
let timer = setInterval(() => {
|
|
|
|
let scrollHeight = document.body.scrollHeight;
|
|
|
|
window.scrollBy(0, distance);
|
|
|
|
totalHeight += distance;
|
|
|
|
|
|
|
|
if (totalHeight >= scrollHeight - window.innerHeight) {
|
|
|
|
clearInterval(timer);
|
|
|
|
resolve();
|
|
|
|
}
|
|
|
|
}, 100);
|
|
|
|
});
|
|
|
|
|
|
|
|
window.scrollTo(0,0);
|
|
|
|
});
|
|
|
|
}
|