Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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
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;
}
}
}
}
async function getOptions() {
if (isDev) {
return {
args: [],
executablePath: exePath,
headless: true
}
}
return {
args: chrome.args,
executablePath: await chrome.executablePath,
headless: chrome.headless
}
}
* @property {"page"|"viewport"|"element"} [capture]
* @property {PuppeteerOptions} [puppeteer]
* @property {string[]} [exclude]
* @property {string} [selector]
* @property {PuppeteerStyleTagOptions[]} [styles]
*/
const defaultViewport = {
width: VIEWPORT_WIDTH,
height: VIEWPORT_HEIGHT,
deviceScaleFactor: DEVICE_SCALE_FACTOR,
isMobile: IS_MOBILE,
isLandscape: IS_LANDSCAPE
};
const puppetshot = chromeLambda.executablePath.then(
executablePath =>
new MrPuppetshot(
{
executablePath,
args: [...chromeLambda.args, ...CHROMIUM_ARGS],
headless: true,
defaultViewport,
ignoreHTTPSErrors: IGNORE_HTTPS_ERRORS,
timeout: TIMEOUT
},
chromeLambda.puppeteer
)
);
/**
* @type {import('aws-lambda').Handler}
const getOptions = async () => {
return process.env.NOW_REGION === "dev1"
? {
args: [],
executablePath: exePath,
headless: true,
}
: {
args: chrome.args,
executablePath: await chrome.executablePath,
headless: chrome.headless
}
}
const createChromeAWSLambdaRenderer = () => async event => {
const command = commands[event.command];
if (!command) {
throw serializeError(new Error(`Unknown command "${event.command}"`));
}
const target = createChromeAppTarget({
baseUrl: event.baseUrl,
});
try {
await target.start({
chromeFlags: chromium.args,
chromePath: await chromium.executablePath,
});
return await command(target, event);
} catch (error) {
throw serializeError(unwrapError(error));
} finally {
await target.stop();
}
};
const takeScreenshot = async (feature, periods, accessibleColours) => {
const browser = await chromium.puppeteer.launch({
args: chromium.args,
defaultViewport: {
width: 800,
height: 600,
isLandscape: true
},
executablePath: await chromium.executablePath,
headless: chromium.headless,
});
const page = await browser.newPage();
await page.goto(
`https://caniuse.bitsofco.de/embed/index.html?feat=${feature}&periods=${periods}&accessible-colours=${accessibleColours}&screenshot=true`,
{ waitUntil: 'networkidle2' }
);
const screenshot = await page.screenshot({
omitBackground: true,
encoding: 'binary'
});
await browser.close();
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;
}
return macro_response;
} else {
var response_object = {
"Status": "SUCCESS",
"PhysicalResourceId": event.LogicalResourceId,
"StackId": event.StackId,
"RequestId": event.RequestId,
"LogicalResourceId": event.LogicalResourceId,
"Data": {}
};
try {
browser = await puppeteer.launch({
args: chromium.args,
defaultViewport: chromium.defaultViewport,
executablePath: await chromium.executablePath,
headless: chromium.headless,
});
let page = await browser.newPage();
if (event.RequestType == "Create" && event.ResourceType == "Custom::AWS_Connect_Instance") {
await login(page);
response_object.Data = await createinstance(page, event.ResourceProperties);
} else if (event.RequestType == "Create" && event.ResourceType == "Custom::AWS_Connect_ContactFlow") {
await login(page);
await open(page, event.ResourceProperties);
response_object.Data = await createflow(page, event.ResourceProperties);
} else if (event.RequestType == "Create" && event.ResourceType == "Custom::AWS_Connect_PhoneNumber") {
await login(page);
await open(page, event.ResourceProperties);
response_object.Data = await claimnumber(page, event.ResourceProperties);