Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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;
}
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' }
);
executablePath =>
new MrPuppetshot(
{
executablePath,
args: [...chromeLambda.args, ...CHROMIUM_ARGS],
headless: true,
defaultViewport,
ignoreHTTPSErrors: IGNORE_HTTPS_ERRORS,
timeout: TIMEOUT
},
chromeLambda.puppeteer
)
);
}
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);
module.exports = async () => {
const config = {
args: chromium.args,
defaultViewport: chromium.defaultViewport,
executablePath: await chromium.executablePath,
headless: chromium.headless,
}
const browser = await chromium.puppeteer.launch(config)
global.__BROWSER__ = browser
await fs.mkdirs(DIR)
await fs.writeFile(path.join(DIR, 'wsEndpoint'), browser.wsEndpoint())
}
export async function runPuppeteer(url: string) {
const browser = await puppeteer.launch({
args: chromium.args,
defaultViewport: chromium.defaultViewport,
executablePath: await chromium.executablePath,
headless: chromium.headless
});
try {
let page = await browser.newPage();
await page.goto(url);
const title = await page.title();
const screenshot = await page.screenshot({ encoding: "binary" });
return { title, screenshot };
} finally {
if (browser !== null) {
await browser.close();
}
}
}
const getScreenshot = async (pageContent: string, targetId: string) => {
const browser = await puppeteer.launch({
args: chrome.args,
executablePath: await chrome.executablePath,
headless: chrome.headless
})
const page = await browser.newPage()
await page.setContent(pageContent)
const rect = await page.evaluate(targetId => {
const element = document.getElementById(targetId)
const {x, y, width, height} = element.getBoundingClientRect() as DOMRect
return {left: x, top: y, width, height, id: element.id}
}, targetId)
const file = await page.screenshot({
type: 'png',
omitBackground: true,
const getOptions = async () => {
return process.env.NOW_REGION === "dev1"
? {
args: [],
executablePath: exePath,
headless: true,
}
: {
args: chrome.args,
executablePath: await chrome.executablePath,
headless: chrome.headless
}
}