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"]
})
await page.waitForSelector('#phenomic')
theTitle = await page.title();
console.log('done on page', theTitle)
const getOptions = async () => {
return process.env.NOW_REGION === "dev1"
? {
args: [],
executablePath: exePath,
headless: true,
}
: {
args: chrome.args,
executablePath: await chrome.executablePath,
headless: chrome.headless
}
}
exports.handler = async (event, context) => {
const pageToScreenshot = JSON.parse(event.body).pageToScreenshot;
if (!pageToScreenshot) return {
statusCode: 400,
body: JSON.stringify({ message: 'Page URL not defined' })
}
const browser = await chromium.puppeteer.launch({
args: chromium.args,
defaultViewport: chromium.defaultViewport,
executablePath: await chromium.executablePath,
headless: chromium.headless,
});
const page = await browser.newPage();
await page.goto(pageToScreenshot, { waitUntil: 'networkidle2' });
const screenshot = await page.screenshot({ encoding: 'binary' });
await browser.close();
return {
statusCode: 200,
body: JSON.stringify({
message: `Complete screenshot of ${pageToScreenshot}`,
buffer: screenshot
})
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;
}
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,
clip: {
x: rect.left - padding,
async function getOptions() {
if (isDev) {
return {
args: [],
executablePath: exePath,
headless: true
}
}
return {
args: chrome.args,
executablePath: await chrome.executablePath,
headless: chrome.headless
}
}
module.exports.screenshot = async (event, context, callback) => {
let browser = null;
try {
browser = await puppeteer.launch({
args: chromium.args,
executablePath: await chromium.executablePath,
headless: chromium.headless,
});
let page = await browser.newPage();
let url = 'https://www.premieroctet.com/';
let color = 'transparent';
if (event.queryStringParameters && event.queryStringParameters.url) {
url = decodeURIComponent(event.queryStringParameters.url);
url = url.toLowerCase();
if (!url.startsWith('http')) {
url = 'http://' + url;
}
}