chore(examples): indent all examples (#1783)

Indented examples are easier to read.
This commit is contained in:
Andrey Lushnikov 2018-01-11 13:42:01 -08:00 committed by GitHub
parent 891a5539bd
commit d68033aeca
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 91 additions and 124 deletions

View File

@ -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"],
}
};

View File

@ -19,20 +19,18 @@
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.setRequestInterception(true);
await page.setRequestInterception(true); page.on('request', request => {
page.on('request', request => {
if (request.resourceType() === 'image') if (request.resourceType() === 'image')
request.abort(); request.abort();
else else
request.continue(); request.continue();
}); });
await page.goto('https://news.google.com/news/'); await page.goto('https://news.google.com/news/');
await page.screenshot({path: 'news.png', fullPage: true}); await page.screenshot({path: 'news.png', fullPage: true});
await browser.close();
await browser.close();
})(); })();

View File

@ -19,32 +19,30 @@
const puppeteer = require('puppeteer'); const puppeteer = require('puppeteer');
(async() => { (async() => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
const browser = await puppeteer.launch(); // Define a window.onCustomEvent function on the page.
const page = await browser.newPage(); await page.exposeFunction('onCustomEvent', e => {
// Define a window.onCustomEvent function on the page.
await page.exposeFunction('onCustomEvent', e => {
console.log(`${e.type} fired`, e.detail || ''); console.log(`${e.type} fired`, e.detail || '');
}); });
/** /**
* Attach an event listener to page to capture a custom event on page load/navigation. * Attach an event listener to page to capture a custom event on page load/navigation.
* @param {string} type Event name. * @param {string} type Event name.
* @return {!Promise} * @return {!Promise}
*/ */
function listenFor(type) { function listenFor(type) {
return page.evaluateOnNewDocument(type => { return page.evaluateOnNewDocument(type => {
document.addEventListener(type, e => { document.addEventListener(type, e => {
window.onCustomEvent({type, detail: e.detail}); window.onCustomEvent({type, detail: e.detail});
}); });
}, type); }, 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 page.goto('https://www.chromestatus.com/features', {waitUntil: 'networkidle2'});
await browser.close();
await browser.close();
})(); })();

View File

@ -34,13 +34,11 @@ function sniffDetector() {
} }
(async() => { (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(); await browser.close();
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();
})(); })();

View File

@ -19,17 +19,15 @@
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: 'networkidle2'});
await page.goto('https://news.ycombinator.com', {waitUntil: 'networkidle2'}); // page.pdf() is currently supported only in headless mode.
// page.pdf() is currently supported only in headless mode. // @see https://bugs.chromium.org/p/chromium/issues/detail?id=753118
// @see https://bugs.chromium.org/p/chromium/issues/detail?id=753118 await page.pdf({
await page.pdf({
path: 'hn.pdf', path: 'hn.pdf',
format: 'letter' format: 'letter'
}); });
await browser.close();
await browser.close();
})(); })();

View File

@ -19,15 +19,13 @@
const puppeteer = require('puppeteer'); const puppeteer = require('puppeteer');
(async() => { (async() => {
const browser = await puppeteer.launch({
const browser = await puppeteer.launch({
// Launch chromium using a proxy server on port 9876. // Launch chromium using a proxy server on port 9876.
// More on proxying: // More on proxying:
// https://www.chromium.org/developers/design-documents/network-settings // https://www.chromium.org/developers/design-documents/network-settings
args: [ '--proxy-server=127.0.0.1:9876' ] args: [ '--proxy-server=127.0.0.1:9876' ]
}); });
const page = await browser.newPage(); const page = await browser.newPage();
await page.goto('https://google.com'); await page.goto('https://google.com');
await browser.close(); await browser.close();
})(); })();

View File

@ -20,12 +20,10 @@ const puppeteer = require('puppeteer');
const devices = require('puppeteer/DeviceDescriptors'); const devices = require('puppeteer/DeviceDescriptors');
(async() => { (async() => {
const browser = await puppeteer.launch();
const browser = await puppeteer.launch(); const page = await browser.newPage();
const page = await browser.newPage(); await page.emulate(devices['iPhone 6']);
await page.emulate(devices['iPhone 6']); await page.goto('https://www.nytimes.com/');
await page.goto('https://www.nytimes.com/'); await page.screenshot({path: 'full.png', fullPage: true});
await page.screenshot({path: 'full.png', fullPage: true}); await browser.close();
await browser.close();
})(); })();

View File

@ -19,12 +19,9 @@
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('http://example.com');
await page.goto('http://example.com'); await page.screenshot({path: 'example.png'});
await page.screenshot({path: 'example.png'}); await browser.close();
await browser.close();
})(); })();

View File

@ -24,34 +24,32 @@
const puppeteer = require('puppeteer'); const puppeteer = require('puppeteer');
(async() => { (async() => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
const browser = await puppeteer.launch(); await page.goto('https://developers.google.com/web/');
const page = await browser.newPage();
await page.goto('https://developers.google.com/web/'); // Type into search box.
await page.type('#searchbox input', 'Headless Chrome');
// Type into search box. // Wait for suggest overlay to appear and click "show all results".
await page.type('#searchbox input', 'Headless Chrome'); 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". // Wait for the results page to load and display the results.
const allResultsSelector = '.devsite-suggest-all-results'; const resultsSelector = '.gsc-results .gsc-thumbnail-inside a.gs-title';
await page.waitForSelector(allResultsSelector); await page.waitForSelector(resultsSelector);
await page.click(allResultsSelector);
// Wait for the results page to load and display the results. // Extract the results from the page.
const resultsSelector = '.gsc-results .gsc-thumbnail-inside a.gs-title'; const links = await page.evaluate(resultsSelector => {
await page.waitForSelector(resultsSelector);
// Extract the results from the page.
const links = await page.evaluate(resultsSelector => {
const anchors = Array.from(document.querySelectorAll(resultsSelector)); const anchors = Array.from(document.querySelectorAll(resultsSelector));
return anchors.map(anchor => { return anchors.map(anchor => {
const title = anchor.textContent.split('|')[0].trim(); const title = anchor.textContent.split('|')[0].trim();
return `${title} - ${anchor.href}`; return `${title} - ${anchor.href}`;
}); });
}, resultsSelector); }, resultsSelector);
console.log(links.join('\n')); console.log(links.join('\n'));
await browser.close();
await browser.close();
})(); })();