mirror of
https://github.com/puppeteer/puppeteer
synced 2024-06-14 14:02:48 +00:00
chore(examples): indent all examples (#1783)
Indented examples are easier to read.
This commit is contained in:
parent
891a5539bd
commit
d68033aeca
@ -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"],
|
||||
}
|
||||
};
|
@ -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();
|
||||
})();
|
||||
|
||||
|
@ -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();
|
||||
})();
|
||||
|
@ -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();
|
||||
})();
|
||||
|
@ -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();
|
||||
})();
|
||||
|
@ -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();
|
||||
})();
|
||||
|
@ -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();
|
||||
})();
|
||||
|
@ -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();
|
||||
})();
|
||||
|
@ -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();
|
||||
})();
|
||||
|
Loading…
Reference in New Issue
Block a user