How to use the puppeteer.launch function in puppeteer

To help you get started, we’ve selected a few puppeteer examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github puppeteer / examples / hash_navigation.js View on Github external
async function main() {
  const browser = await puppeteer.launch();

  const page = await browser.newPage();

  // Catch + "forward" hashchange events from page to node puppeteer.
  await page.exposeFunction('onHashChange', url => page.emit('hashchange', url));
  await page.evaluateOnNewDocument(() => {
    addEventListener('hashchange', e => onHashChange(location.href));
  });

  // Listen for hashchange events in node Puppeteer code.
  page.on('hashchange', url => console.log('hashchange event:', new URL(url).hash));

  await page.goto(url);
  await page.waitForSelector('[data-page="#page1"]'); // wait for view 1 to be "loaded".
  await printVisibleView(page);
github GoogleChromeLabs / proxx / lib / render-static.js View on Github external
async function prerender(bundle, html) {
  // Make all scripts in the page inert. These are the non-preload scripts,
  // we don't want to run them.
  const noScriptHTML = html.replace(/<(\/?)script/g, "<$1noscript");

  const browser = await puppeteer.launch();
  const page = await browser.newPage();
  page.viewport(1280, 720);
  page.on("pageerror", err => console.error("static-plugin-error", err));

  // prerenderDone is called by our main component on first mount.
  const done = page.evaluate(
    () =>
      new Promise(resolve => {
        window.prerenderDone = resolve;
      })
  );

  await page.setContent(noScriptHTML);

  // This is the prerender version of the JS.
  const scriptToRun = getCodeAndDependencies(bundle, "/main/bootstrap.tsx");
github DanielMSchmidt / zipkin-javascript-opentracing / e2e-tests / react / __tests__ / 1-basic.js View on Github external
beforeAll(async () => {
    browser = await puppeteer.launch();
    page = await browser.newPage();
  });
github xiaohanyu / blog-html-to-pdf / index.js View on Github external
async function main() {
  if (process.argv.length != 4) {
    console.log("error!");
    console.log("usage: node index.js  ")
    process.exit(-1);
  }

  let pdfOutputDir = process.argv[2];
  fs.mkdirSync(pdfOutputDir, { recursive: true });

  const urls = fs
    .readFileSync(process.argv[3])
    .toString()
    .split("\n");

  const browser = await puppeteer.launch();

  for (let url of urls) {
    captureToPDF(browser, url, pdfOutputDir);
    await sleep(5000);
  }

  await browser.close();
}
github cleverage / daltons / src / browse.js View on Github external
module.exports = async function browse(opt) {
  const VIEWPORT = {
    width: opt.minViewport,
    height: 2000,
    deviceScaleFactor: 1,
  }
  const imageWidths = new Map()

  logger.info(color.green('Launch headless Chrome'))

  const browser = await puppeteer.launch()
  const page = await browser.newPage()

  logger.info(color.green(`Go to ${opt.url}`))

  await page
    .goto(opt.url, { waitUntil: 'networkidle2' })
    .then(async () => {
      logger.info(
        color.green(`Checking widths of image ${color.white(opt.selector)}`),
      )
      const spinner = logger.newSpinner()
      if (spinner) {
        spinner.start('Starting…')
      }

      while (VIEWPORT.width <= opt.maxViewport) {
github pioug / cinelah / functions / scraper.js View on Github external
async function getHtmlBody(url, timer = 0) {
  const browser = await puppeteer.launch({
    args: ["--no-sandbox"],
    timeout: 0
  });
  const page = await browser.newPage();
  await page.goto(url);
  await page.waitFor(timer);
  try {
    const body = await page.evaluate(() => document.body.innerHTML);
    await browser.close();
    return body;
  } catch (err) {
    const page = await browser.newPage();
    await page.goto(url);
    await page.waitFor(timer);
    const body = await page.evaluate(() => document.body.innerHTML);
    await browser.close();
github bWhirring / -node-slider-verify / qianduanwang / index.js View on Github external
async function run() {
  const browser = await puppeteer.launch({
    headless: false
  });
  page = (await browser.pages())[0];

  await page.goto("https://www.qdfuns.com/");
  await page.waitForSelector(".hand");

  await page.click("a[data-type=login]");
  const geetest_btn = ".geetest_btn";
  await page.waitForSelector(geetest_btn);

  await page.click(geetest_btn);
  await page.waitFor(1000);

  async function getDistance() {
    let { bg, fullbg } = await page.evaluate(() => {
github cncf / landscapeapp / tools / checkLinks.js View on Github external
async function quickCheckViaPuppeteer(remainingAttempts = 3) {
    const puppeteer = require('puppeteer');
    const browser = await puppeteer.launch({args: ['--no-sandbox', '--disable-setuid-sandbox', '--ignore-certificate-errors']});
    const page = await browser.newPage();
    page.setViewport({width: 1920, height: 1024});
    await page.setRequestInterception(true);
    page.on('request', request => {
      if (request.resourceType() !== 'document')
        request.abort();
      else
        request.continue();
    });
    page.setDefaultNavigationTimeout(120 * 1000);
    let result = null;

    try {
      page.on('response', response => {
        const status = response.status()
        if (url === response.request().url() || url + '/' === response.request().url) {
github shzlw / poli / export-server / poli-export-server.js View on Github external
async function generatePDF(exportRequest) {
  const tomorrow = new Date();
  tomorrow.setDate(tomorrow.getDate() + 1);

  const width = parseInt(exportRequest.width, 10);
  const height = parseInt(exportRequest.height, 10);

  const browser = await puppeteer.launch();
  const page = await browser.newPage();
  const cookie = {
    name: 'pskey',
    value: exportRequest.sessionKey,
    domain: 'localhost',
    path: '/',
    expires: Math.floor(tomorrow.getTime() / 1000)
  };
  await page.setCookie(cookie);
  await page.setViewport({ width: width, height: height });
  await page.goto(poli + '/workspace/report/fullscreen?$showControl=false&$toReport=' + exportRequest.reportName, {waitUntil: 'networkidle2'});
  await page.waitFor(2000);
  const pdf = await page.pdf({
    width: width,
    height: height,
    printBackground: true