2020-10-16 08:06:03 +00:00
|
|
|
"use strict";
|
2020-10-02 12:01:51 +00:00
|
|
|
|
2020-10-01 19:13:23 +00:00
|
|
|
// Public modules from npm
|
2020-10-16 08:06:03 +00:00
|
|
|
const puppeteer = require("puppeteer");
|
2020-10-01 19:13:23 +00:00
|
|
|
|
|
|
|
// Modules from file
|
2020-10-16 08:06:03 +00:00
|
|
|
const shared = require("./shared.js");
|
2020-10-01 19:13:23 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @protected
|
2020-10-12 12:12:29 +00:00
|
|
|
* Create a Chromium instance used to navigate with Puppeteer.
|
2020-10-01 19:13:23 +00:00
|
|
|
* By default the browser is headless.
|
|
|
|
* @returns {Promise<puppeteer.Browser>} Created browser
|
|
|
|
*/
|
2020-10-12 12:12:29 +00:00
|
|
|
module.exports.prepareBrowser = async function () {
|
2020-10-29 21:14:40 +00:00
|
|
|
// Create a headless browser
|
|
|
|
let browser = null;
|
|
|
|
if (shared.chromiumLocalPath) {
|
|
|
|
browser = await puppeteer.launch({
|
|
|
|
executablePath: shared.chromiumLocalPath,
|
|
|
|
headless: !shared.debug, // Use GUI when debug = true
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
browser = await puppeteer.launch({
|
|
|
|
headless: !shared.debug, // Use GUI when debug = true
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return browser;
|
2020-10-12 12:12:29 +00:00
|
|
|
};
|
2020-10-01 19:13:23 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @protected
|
|
|
|
* Prepare a page used to navigate the browser.
|
|
|
|
* The page is set up to reject image download requests. The user agent is also changed.
|
|
|
|
* @param {puppeteer.Browser} browser Browser to use when navigating where the page will be created
|
|
|
|
* @returns {Promise<puppeteer.Page>} New page
|
|
|
|
*/
|
2020-10-12 12:12:29 +00:00
|
|
|
module.exports.preparePage = async function (browser) {
|
2020-10-29 21:14:40 +00:00
|
|
|
// Create new page in the browser argument
|
|
|
|
const page = await browser.newPage();
|
|
|
|
|
|
|
|
// Block image download
|
|
|
|
await page.setRequestInterception(true);
|
|
|
|
page.on("request", (request) => {
|
|
|
|
if (request.resourceType() === "image") request.abort();
|
|
|
|
else if (request.resourceType === "font") request.abort();
|
|
|
|
// else if (request.resourceType() == 'stylesheet') request.abort();
|
|
|
|
// else if(request.resourceType == 'media') request.abort();
|
|
|
|
else request.continue();
|
|
|
|
});
|
|
|
|
|
|
|
|
// Set custom user-agent
|
|
|
|
const userAgent =
|
2020-10-16 08:06:03 +00:00
|
|
|
"Mozilla/5.0 (X11; Linux x86_64)" +
|
|
|
|
"AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.39 Safari/537.36";
|
2020-10-29 21:14:40 +00:00
|
|
|
await page.setUserAgent(userAgent);
|
2020-10-12 12:12:29 +00:00
|
|
|
|
2020-10-29 21:14:40 +00:00
|
|
|
return page;
|
2020-10-12 12:12:29 +00:00
|
|
|
};
|