Code Monkey home page Code Monkey logo

try-puppeteer's Introduction

Try Puppeteer!

Run Puppeteer scripts in the cloud.

Try it: https://try-puppeteer.appspot.com

Develop

Installation:

yarn; yarn install-backend
# or npm i

Backend

The backend is a Docker container which installs the latest Chrome package that works with Puppeteer on Linux.

Note: You'll need to have Docker running before attempting each step in this section.

Building it

yarn build

Running the container

The container can be run in two modes, standalone as an executable or as a web service.

1. Using the standalone CLI

The first is a "standalone" mode that you can from a Puppeteer script from the CLI. It takes a script file as an argument and runs it in the container.

./backend/run_puppeteer.sh your-puppeteer-script.js

2. Running the web service

The second option is running the container as a web server. The endpoint accepts file uploads for running your Puppeteer scripts in the cloud:

Start the server:

cd backend
yarn serve
# yarn restart is handy too. It rebuilds the container and starts the server.

Example - running a Puppeteer script

async function runCode(code) {
  const form = new FormData();
  form.append('file', new Blob([code], {type: 'text/javascript'}));
  const resp = await fetch('http://localhost:8080/run', {method: 'POST', body: form});
  return await resp.json();
}

const code = `
  const browser = await puppeteer.launch();
  const page = await browser.newPage();
  await page.goto('https://example.com');
  console.log(await page.content());
  browser.close();
`;

runCode(code).then(result => {
  if (result.errors) {
    console.error(result.errors);
  }
  console.log(result.log);
});

Notes:

  • There's no need to require('puppeteer'). This is done for you in on the backend.
  • Top-level async/await are supported.

Code editor frontend

Fire up the code editor UI from the main directory:

yarn serve

Then navigate to http://localhost:8081.

Deployment

  1. Update the version of Puppeteer used in index.html, include the doc link. TODO: make this automatic.

  2. yarn deploy deploys both the frontend and backend services to App Engine Flex. The apps can also be deployed individually:

yarn deploy-frontend
yarn deploy-backend

Notes & Limitations

  • By default, Puppeteer launches and uses it's own bundled version of Chromium. To use the google-chrome-unstable installed by the container, pass executablePath:

    const browser = await puppeteer.launch({
      executablePath: 'google-chrome-unstable'
    });

try-puppeteer's People

Contributors

ebidel avatar

Stargazers

Robert Christian avatar

Watchers

James Cloos avatar Robert Christian avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.