2020-10-12 12:12:29 +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-12 12:12:29 +00:00
|
|
|
const puppeteer = require("puppeteer");
|
2020-10-01 19:13:23 +00:00
|
|
|
|
|
|
|
// Modules from file
|
2020-10-12 12:12:29 +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 () {
|
|
|
|
// Create a headless browser
|
2020-10-14 13:12:35 +00:00
|
|
|
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
|
|
|
|
});
|
|
|
|
}
|
2020-10-14 13:15:34 +00:00
|
|
|
|
2020-10-12 12:12:29 +00:00
|
|
|
return browser;
|
|
|
|
};
|
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) {
|
|
|
|
// Create new page in the browser argument
|
|
|
|
let 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
|
|
|
|
let userAgent =
|
|
|
|
"Mozilla/5.0 (X11; Linux x86_64)" +
|
|
|
|
"AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.39 Safari/537.36";
|
|
|
|
await page.setUserAgent(userAgent);
|
|
|
|
|
|
|
|
return page;
|
|
|
|
};
|