From d68033aeca234a93c9ac5298258a5d324748466a Mon Sep 17 00:00:00 2001 From: Andrey Lushnikov Date: Thu, 11 Jan 2018 13:42:01 -0800 Subject: [PATCH] chore(examples): indent all examples (#1783) Indented examples are easier to read. --- examples/.eslintrc.js | 16 ------------ examples/block-images.js | 26 +++++++++---------- examples/custom-event.js | 44 +++++++++++++++---------------- examples/detect-sniff.js | 14 +++++----- examples/pdf.js | 22 +++++++--------- examples/proxy.js | 20 +++++++------- examples/screenshot-fullpage.js | 14 +++++----- examples/screenshot.js | 13 ++++------ examples/search.js | 46 ++++++++++++++++----------------- 9 files changed, 91 insertions(+), 124 deletions(-) delete mode 100644 examples/.eslintrc.js diff --git a/examples/.eslintrc.js b/examples/.eslintrc.js deleted file mode 100644 index 23549783..00000000 --- a/examples/.eslintrc.js +++ /dev/null @@ -1,16 +0,0 @@ -module.exports = { - "extends": "../.eslintrc.js", - /** - * ESLint rules - * - * All available rules: http://eslint.org/docs/rules/ - * - * Rules take the following form: - * "rule-name", [severity, { opts }] - * Severity: 2 == error, 1 == warning, 0 == off. - */ - "rules": { - "indent": [2, 2, { "SwitchCase": 1, "CallExpression": {"arguments": 2}, "MemberExpression": 2, "outerIIFEBody": 0 }], - "strict": [2, "global"], - } -}; diff --git a/examples/block-images.js b/examples/block-images.js index 51acd6dc..cbad0abc 100644 --- a/examples/block-images.js +++ b/examples/block-images.js @@ -19,20 +19,18 @@ const puppeteer = require('puppeteer'); (async() => { + const browser = await puppeteer.launch(); + const page = await browser.newPage(); + await page.setRequestInterception(true); + page.on('request', request => { + if (request.resourceType() === 'image') + request.abort(); + else + request.continue(); + }); + await page.goto('https://news.google.com/news/'); + await page.screenshot({path: 'news.png', fullPage: true}); -const browser = await puppeteer.launch(); -const page = await browser.newPage(); -await page.setRequestInterception(true); -page.on('request', request => { - if (request.resourceType() === 'image') - request.abort(); - else - request.continue(); -}); -await page.goto('https://news.google.com/news/'); -await page.screenshot({path: 'news.png', fullPage: true}); - -await browser.close(); - + await browser.close(); })(); diff --git a/examples/custom-event.js b/examples/custom-event.js index 527612ad..d7286387 100644 --- a/examples/custom-event.js +++ b/examples/custom-event.js @@ -19,32 +19,30 @@ const puppeteer = require('puppeteer'); (async() => { + const browser = await puppeteer.launch(); + const page = await browser.newPage(); -const browser = await puppeteer.launch(); -const page = await browser.newPage(); + // Define a window.onCustomEvent function on the page. + await page.exposeFunction('onCustomEvent', e => { + console.log(`${e.type} fired`, e.detail || ''); + }); -// Define a window.onCustomEvent function on the page. -await page.exposeFunction('onCustomEvent', e => { - console.log(`${e.type} fired`, e.detail || ''); -}); + /** + * Attach an event listener to page to capture a custom event on page load/navigation. + * @param {string} type Event name. + * @return {!Promise} + */ + function listenFor(type) { + return page.evaluateOnNewDocument(type => { + document.addEventListener(type, e => { + window.onCustomEvent({type, detail: e.detail}); + }); + }, type); + } -/** - * Attach an event listener to page to capture a custom event on page load/navigation. - * @param {string} type Event name. - * @return {!Promise} - */ -function listenFor(type) { - return page.evaluateOnNewDocument(type => { - document.addEventListener(type, e => { - window.onCustomEvent({type, detail: e.detail}); - }); - }, type); -} + await listenFor('app-ready'); // Listen for "app-ready" custom event on page load. -await listenFor('app-ready'); // Listen for "app-ready" custom event on page load. - -await page.goto('https://www.chromestatus.com/features', {waitUntil: 'networkidle2'}); - -await browser.close(); + await page.goto('https://www.chromestatus.com/features', {waitUntil: 'networkidle2'}); + await browser.close(); })(); diff --git a/examples/detect-sniff.js b/examples/detect-sniff.js index b8b37ad2..7a35dd0b 100644 --- a/examples/detect-sniff.js +++ b/examples/detect-sniff.js @@ -34,13 +34,11 @@ function sniffDetector() { } (async() => { + const browser = await puppeteer.launch(); + const page = await browser.newPage(); + await page.evaluateOnNewDocument(sniffDetector); + await page.goto('https://www.google.com', {waitUntil: 'networkidle2'}); + console.log('Sniffed: ' + (await page.evaluate(() => !!navigator.sniffed))); -const browser = await puppeteer.launch(); -const page = await browser.newPage(); -await page.evaluateOnNewDocument(sniffDetector); -await page.goto('https://www.google.com', {waitUntil: 'networkidle2'}); -console.log('Sniffed: ' + (await page.evaluate(() => !!navigator.sniffed))); - -await browser.close(); - + await browser.close(); })(); diff --git a/examples/pdf.js b/examples/pdf.js index a31de285..c2c9f5e7 100644 --- a/examples/pdf.js +++ b/examples/pdf.js @@ -19,17 +19,15 @@ const puppeteer = require('puppeteer'); (async() => { + const browser = await puppeteer.launch(); + const page = await browser.newPage(); + await page.goto('https://news.ycombinator.com', {waitUntil: 'networkidle2'}); + // page.pdf() is currently supported only in headless mode. + // @see https://bugs.chromium.org/p/chromium/issues/detail?id=753118 + await page.pdf({ + path: 'hn.pdf', + format: 'letter' + }); -const browser = await puppeteer.launch(); -const page = await browser.newPage(); -await page.goto('https://news.ycombinator.com', {waitUntil: 'networkidle2'}); -// page.pdf() is currently supported only in headless mode. -// @see https://bugs.chromium.org/p/chromium/issues/detail?id=753118 -await page.pdf({ - path: 'hn.pdf', - format: 'letter' -}); - -await browser.close(); - + await browser.close(); })(); diff --git a/examples/proxy.js b/examples/proxy.js index 31d014bb..6ff63d1f 100644 --- a/examples/proxy.js +++ b/examples/proxy.js @@ -19,15 +19,13 @@ const puppeteer = require('puppeteer'); (async() => { - -const browser = await puppeteer.launch({ - // Launch chromium using a proxy server on port 9876. - // More on proxying: - // https://www.chromium.org/developers/design-documents/network-settings - args: [ '--proxy-server=127.0.0.1:9876' ] -}); -const page = await browser.newPage(); -await page.goto('https://google.com'); -await browser.close(); - + const browser = await puppeteer.launch({ + // Launch chromium using a proxy server on port 9876. + // More on proxying: + // https://www.chromium.org/developers/design-documents/network-settings + args: [ '--proxy-server=127.0.0.1:9876' ] + }); + const page = await browser.newPage(); + await page.goto('https://google.com'); + await browser.close(); })(); diff --git a/examples/screenshot-fullpage.js b/examples/screenshot-fullpage.js index 6d39fac1..fcb24ab0 100644 --- a/examples/screenshot-fullpage.js +++ b/examples/screenshot-fullpage.js @@ -20,12 +20,10 @@ const puppeteer = require('puppeteer'); const devices = require('puppeteer/DeviceDescriptors'); (async() => { - -const browser = await puppeteer.launch(); -const page = await browser.newPage(); -await page.emulate(devices['iPhone 6']); -await page.goto('https://www.nytimes.com/'); -await page.screenshot({path: 'full.png', fullPage: true}); -await browser.close(); - + const browser = await puppeteer.launch(); + const page = await browser.newPage(); + await page.emulate(devices['iPhone 6']); + await page.goto('https://www.nytimes.com/'); + await page.screenshot({path: 'full.png', fullPage: true}); + await browser.close(); })(); diff --git a/examples/screenshot.js b/examples/screenshot.js index d8271bab..25fe931b 100644 --- a/examples/screenshot.js +++ b/examples/screenshot.js @@ -19,12 +19,9 @@ const puppeteer = require('puppeteer'); (async() => { - -const browser = await puppeteer.launch(); -const page = await browser.newPage(); -await page.goto('http://example.com'); -await page.screenshot({path: 'example.png'}); - -await browser.close(); - + const browser = await puppeteer.launch(); + const page = await browser.newPage(); + await page.goto('http://example.com'); + await page.screenshot({path: 'example.png'}); + await browser.close(); })(); diff --git a/examples/search.js b/examples/search.js index 3b7d1f19..4845ab5d 100644 --- a/examples/search.js +++ b/examples/search.js @@ -24,34 +24,32 @@ const puppeteer = require('puppeteer'); (async() => { + const browser = await puppeteer.launch(); + const page = await browser.newPage(); -const browser = await puppeteer.launch(); -const page = await browser.newPage(); + await page.goto('https://developers.google.com/web/'); -await page.goto('https://developers.google.com/web/'); + // Type into search box. + await page.type('#searchbox input', 'Headless Chrome'); -// Type into search box. -await page.type('#searchbox input', 'Headless Chrome'); + // Wait for suggest overlay to appear and click "show all results". + const allResultsSelector = '.devsite-suggest-all-results'; + await page.waitForSelector(allResultsSelector); + await page.click(allResultsSelector); -// Wait for suggest overlay to appear and click "show all results". -const allResultsSelector = '.devsite-suggest-all-results'; -await page.waitForSelector(allResultsSelector); -await page.click(allResultsSelector); + // Wait for the results page to load and display the results. + const resultsSelector = '.gsc-results .gsc-thumbnail-inside a.gs-title'; + await page.waitForSelector(resultsSelector); -// Wait for the results page to load and display the results. -const resultsSelector = '.gsc-results .gsc-thumbnail-inside a.gs-title'; -await page.waitForSelector(resultsSelector); - -// Extract the results from the page. -const links = await page.evaluate(resultsSelector => { - const anchors = Array.from(document.querySelectorAll(resultsSelector)); - return anchors.map(anchor => { - const title = anchor.textContent.split('|')[0].trim(); - return `${title} - ${anchor.href}`; - }); -}, resultsSelector); -console.log(links.join('\n')); - -await browser.close(); + // Extract the results from the page. + const links = await page.evaluate(resultsSelector => { + const anchors = Array.from(document.querySelectorAll(resultsSelector)); + return anchors.map(anchor => { + const title = anchor.textContent.split('|')[0].trim(); + return `${title} - ${anchor.href}`; + }); + }, resultsSelector); + console.log(links.join('\n')); + await browser.close(); })();