Make prettier JS snippets (#377)

* Make prettier JS snippets

* Conform to CS (about curly brace)

* Apply feedback
This commit is contained in:
Brikou CARRE 2017-08-19 01:46:10 +02:00 committed by Eric Bidelman
parent ae3fedfa91
commit 8f43befcc9
2 changed files with 12 additions and 8 deletions

View File

@ -42,7 +42,6 @@ of `Browser`, open pages, and then manipulate them with [Puppeteer's API](https:
const puppeteer = require('puppeteer'); const puppeteer = require('puppeteer');
(async () => { (async () => {
const browser = await puppeteer.launch(); const browser = await puppeteer.launch();
const page = await browser.newPage(); const page = await browser.newPage();
await page.goto('https://example.com'); await page.goto('https://example.com');
@ -60,7 +59,6 @@ Puppeteer sets an initial page size to 800px x 600px, which defines the screensh
const puppeteer = require('puppeteer'); const puppeteer = require('puppeteer');
(async () => { (async () => {
const browser = await puppeteer.launch(); const browser = await puppeteer.launch();
const page = await browser.newPage(); const page = await browser.newPage();
await page.goto('https://news.ycombinator.com', {waitUntil: 'networkidle'}); await page.goto('https://news.ycombinator.com', {waitUntil: 'networkidle'});

View File

@ -221,7 +221,7 @@ The arguments passed into `console.log` appear as arguments on the event handler
An example of handling `console` event: An example of handling `console` event:
```js ```js
page.on('console', (...args) => { page.on('console', (...args) => {
for (let i =0; i < args.length; ++i) for (let i = 0; i < args.length; ++i)
console.log(`${i}: ${args[i]}`); console.log(`${i}: ${args[i]}`);
}); });
page.evaluate(() => console.log(5, 'hello', {foo: 'bar'})); page.evaluate(() => console.log(5, 'hello', {foo: 'bar'}));
@ -406,7 +406,9 @@ const crypto = require('crypto');
puppeteer.launch().then(async browser => { puppeteer.launch().then(async browser => {
let page = await browser.newPage(); let page = await browser.newPage();
page.on('console', console.log); page.on('console', console.log);
await page.exposeFunction('md5', text => crypto.createHash('md5').update(text).digest('hex')); await page.exposeFunction('md5', text =>
crypto.createHash('md5').update(text).digest('hex')
);
await page.evaluate(async () => { await page.evaluate(async () => {
// use window.md5 to compute hashes // use window.md5 to compute hashes
let myString = 'PUPPETEER'; let myString = 'PUPPETEER';
@ -761,7 +763,9 @@ const puppeteer = require('puppeteer');
puppeteer.launch().then(async browser => { puppeteer.launch().then(async browser => {
let page = await browser.newPage(); let page = await browser.newPage();
let currentURL; let currentURL;
page.waitForSelector('img').then(() => console.log('First URL with image: ' + currentURL)); page
.waitForSelector('img')
.then(() => console.log('First URL with image: ' + currentURL));
for (currentURL of ['https://example.com', 'https://google.com', 'https://bbc.com']) for (currentURL of ['https://example.com', 'https://google.com', 'https://bbc.com'])
await page.goto(currentURL); await page.goto(currentURL);
browser.close(); browser.close();
@ -1065,7 +1069,9 @@ const puppeteer = require('puppeteer');
puppeteer.launch().then(async browser => { puppeteer.launch().then(async browser => {
let page = await browser.newPage(); let page = await browser.newPage();
let currentURL; let currentURL;
page.waitForSelector('img').then(() => console.log('First URL with image: ' + currentURL)); page
.waitForSelector('img')
.then(() => console.log('First URL with image: ' + currentURL));
for (currentURL of ['https://example.com', 'https://google.com', 'https://bbc.com']) for (currentURL of ['https://example.com', 'https://google.com', 'https://bbc.com'])
await page.goto(currentURL); await page.goto(currentURL);
browser.close(); browser.close();
@ -1083,7 +1089,7 @@ puppeteer.launch().then(async browser => {
await page.goto('https://google.com'); await page.goto('https://google.com');
let inputElement = await page.$('input[type=submit]'); let inputElement = await page.$('input[type=submit]');
await inputElement.click(); await inputElement.click();
... // ...
}); });
``` ```