Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
let actionDone = false;
const width = parseInt(searchParams.get('width'), 10) || 1024;
const height = parseInt(searchParams.get('height'), 10) || 768;
page = cache.get(pageURL);
if (!page) {
if (!browser) {
console.log('🚀 Launch browser!');
const config = {
...(isDev ? {
// executablePath: '/Applications/Google Chrome Canary.app/Contents/MacOS/Google Chrome Canary',
executablePath: findChrome(),
} : {
args: chrome.args,
executablePath: await chrome.executablePath,
headless: chrome.headless,
}),
};
browser = await puppeteer.launch(config);
}
page = await browser.newPage();
const nowTime = +new Date();
let reqCount = 0;
await page.setRequestInterception(true);
page.on('request', (request) => {
const url = request.url();
const method = request.method();
const resourceType = request.resourceType();
// Skip data URIs
if (/^data:/i.test(url)){
exports.handler = async (event, context, callback) => {
let theTitle = null
let browser = null
console.log('spawning chrome headless')
try {
const executablePath = await chromium.executablePath
// setup
browser = await puppeteer.launch({
args: chromium.args,
executablePath: executablePath,
headless: chromium.headless,
})
// Do stuff with headless chrome
const page = await browser.newPage()
const targetUrl = 'https://davidwells.io'
// Goto page and then do stuff
await page.goto(targetUrl, {
waitUntil: ["domcontentloaded", "networkidle0"]
})
let actionDone = false;
const width = parseInt(searchParams.get('width'), 10) || 1024;
const height = parseInt(searchParams.get('height'), 10) || 768;
page = cache.get(pageURL);
if (!page) {
if (!browser) {
console.log('🚀 Launch browser!');
const config = {
...(isDev ? {
// executablePath: '/Applications/Google Chrome Canary.app/Contents/MacOS/Google Chrome Canary',
executablePath: findChrome(),
} : {
args: chrome.args,
executablePath: await chrome.executablePath,
headless: chrome.headless,
}),
};
browser = await puppeteer.launch(config);
}
page = await browser.newPage();
const nowTime = +new Date();
let reqCount = 0;
await page.setRequestInterception(true);
page.on('request', (request) => {
const url = request.url();
const method = request.method();
const resourceType = request.resourceType();
// Skip data URIs
exports.handler = async (event, context, callback) => {
let theTitle = null
let browser = null
console.log('spawning chrome headless')
try {
const executablePath = await chromium.executablePath
// setup
browser = await puppeteer.launch({
args: chromium.args,
executablePath: executablePath,
headless: chromium.headless,
})
// Do stuff with headless chrome
const page = await browser.newPage()
const targetUrl = 'https://davidwells.io'
// Goto page and then do stuff
await page.goto(targetUrl, {
waitUntil: ["domcontentloaded", "networkidle0"]
})
await page.waitForSelector('#phenomic')
theTitle = await page.title();
console.log('done on page', theTitle)
const getPuppeteer = async () => {
try {
const puppeteer = require('puppeteer');
return await puppeteer.launch();
} catch (error) {
if (error.code === 'MODULE_NOT_FOUND') {
console.log('Error(This package is used for local development) ', JSON.stringify(error, null, 2));
try {
const chromium = require('chrome-aws-lambda')
return await chromium.puppeteer.launch({
args: chromium.args,
defaultViewport: chromium.defaultViewport,
executablePath: await chromium.executablePath,
headless: chromium.headless
});
} catch (_error) {
throw error;
}
}
}
}
const getPuppeteer = async () => {
try {
const puppeteer = require('puppeteer');
return await puppeteer.launch();
} catch (error) {
if (error.code === 'MODULE_NOT_FOUND') {
console.log('Error(This package is used for local development) ', JSON.stringify(error, null, 2));
try {
const chromium = require('chrome-aws-lambda')
return await chromium.puppeteer.launch({
args: chromium.args,
defaultViewport: chromium.defaultViewport,
executablePath: await chromium.executablePath,
headless: chromium.headless
});
} catch (_error) {
throw error;
}
}
}
}
const getPuppeteer = async () => {
try {
const puppeteer = require('puppeteer');
return await puppeteer.launch();
} catch (error) {
if (error.code === 'MODULE_NOT_FOUND') {
console.log('Error(This package is used for local development) ', JSON.stringify(error, null, 2));
try {
const chromium = require('chrome-aws-lambda')
return await chromium.puppeteer.launch({
args: chromium.args,
defaultViewport: chromium.defaultViewport,
executablePath: await chromium.executablePath,
headless: chromium.headless
});
} catch (_error) {
throw error;
}
}
}
}
const getPuppeteer = async () => {
try {
const puppeteer = require('puppeteer');
return await puppeteer.launch();
} catch (error) {
if (error.code === 'MODULE_NOT_FOUND') {
console.log('Error(This package is used for local development) ', JSON.stringify(error, null, 2));
try {
const chromium = require('chrome-aws-lambda')
return await chromium.puppeteer.launch({
args: chromium.args,
defaultViewport: chromium.defaultViewport,
executablePath: await chromium.executablePath,
headless: chromium.headless
});
} catch (_error) {
throw error;
}
}
}
}
export async function getOptions(isDev: boolean) {
let options: Options;
if (isDev) {
options = {
args: [],
executablePath: exePath,
headless: true
};
} else {
options = {
args: chrome.args,
executablePath: await chrome.executablePath,
headless: chrome.headless,
};
}
return options;
}
async function getScreenshot(url, type, quality, fullPage) {
const browser = await puppeteer.launch({
args: chrome.args,
executablePath: await chrome.executablePath,
headless: chrome.headless,
});
const page = await browser.newPage();
await page.goto(url);
const file = await page.screenshot({ type, quality, fullPage });
await browser.close();
return file;
}