mirror of
https://github.com/puppeteer/puppeteer
synced 2024-06-14 14:02:48 +00:00
[examples] get rid of old examples
This patch: - removes old phantom-js examples - adds an example to load page without images References #178
This commit is contained in:
parent
104455c75a
commit
29adc5dc80
@ -23,7 +23,7 @@ To add Puppeteer to your project, run:
|
|||||||
yarn add puppeteer
|
yarn add puppeteer
|
||||||
```
|
```
|
||||||
|
|
||||||
> **NOTE** Puppeteer bundles a version of Chromium (~90Mb) which it is guaranteed to work with. However, you're free to point Puppeteer to any Chromium executable ([example](https://github.com/GoogleChrome/puppeteer/blob/master/examples/custom-chromium-revision.js))
|
> **NOTE** Puppeteer bundles a version of Chromium (~90Mb) which it is guaranteed to work with. However, you're free to point Puppeteer to any Chromium executable ([documentation](https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#new-browseroptions))
|
||||||
|
|
||||||
|
|
||||||
## Getting Started
|
## Getting Started
|
||||||
|
@ -14,25 +14,21 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
const Browser = require('../lib/Browser');
|
(async() => {
|
||||||
|
|
||||||
if (process.argv.length < 3) {
|
const {Browser} = require('puppeteer');
|
||||||
console.log('Usage: loadurlwithoutcss.js URL');
|
const browser = new Browser();
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
let address = process.argv[2];
|
const page = await browser.newPage();
|
||||||
|
await page.setRequestInterceptor(request => {
|
||||||
let browser = new Browser({headless: false});
|
if (/\.(png|jpg|jpeg$)/.test(request.url))
|
||||||
browser.newPage().then(async page => {
|
request.abort();
|
||||||
page.setRequestInterceptor(request => {
|
else
|
||||||
if (request.url.endsWith('.css'))
|
request.continue();
|
||||||
request.abort();
|
|
||||||
else
|
|
||||||
request.continue();
|
|
||||||
});
|
|
||||||
let success = await page.navigate(address);
|
|
||||||
if (!success)
|
|
||||||
console.log('Unable to load the address!');
|
|
||||||
browser.close();
|
|
||||||
});
|
});
|
||||||
|
await page.navigate('https://bbc.com');
|
||||||
|
await page.screenshot({path: 'news.png', fullPage: true});
|
||||||
|
browser.close();
|
||||||
|
|
||||||
|
})();
|
||||||
|
|
@ -1,71 +0,0 @@
|
|||||||
/**
|
|
||||||
* Copyright 2017 Google Inc., PhantomJS Authors All rights reserved.
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
|
|
||||||
const Browser = require('../lib/Browser');
|
|
||||||
let browser = new Browser();
|
|
||||||
|
|
||||||
browser.newPage().then(async page => {
|
|
||||||
await page.setViewport({width: 400, height: 400});
|
|
||||||
await page.setContent('<html><body><canvas id="surface"></canvas></body></html>');
|
|
||||||
await page.evaluate(drawColorWheel);
|
|
||||||
await page.screenshot({path: 'colorwheel.png'});
|
|
||||||
browser.close();
|
|
||||||
});
|
|
||||||
|
|
||||||
function drawColorWheel() {
|
|
||||||
let el = document.getElementById('surface'),
|
|
||||||
context = el.getContext('2d'),
|
|
||||||
width = window.innerWidth,
|
|
||||||
height = window.innerHeight,
|
|
||||||
cx = width / 2,
|
|
||||||
cy = height / 2,
|
|
||||||
radius = width / 2.3,
|
|
||||||
imageData,
|
|
||||||
pixels,
|
|
||||||
hue, sat,
|
|
||||||
i = 0, x, y, rx, ry, d,
|
|
||||||
f, g, u, v, w;
|
|
||||||
|
|
||||||
el.width = width;
|
|
||||||
el.height = height;
|
|
||||||
imageData = context.createImageData(width, height);
|
|
||||||
pixels = imageData.data;
|
|
||||||
|
|
||||||
for (y = 0; y < height; y = y + 1) {
|
|
||||||
for (x = 0; x < width; x = x + 1, i = i + 4) {
|
|
||||||
rx = x - cx;
|
|
||||||
ry = y - cy;
|
|
||||||
d = rx * rx + ry * ry;
|
|
||||||
if (d < radius * radius) {
|
|
||||||
hue = 6 * (Math.atan2(ry, rx) + Math.PI) / (2 * Math.PI);
|
|
||||||
sat = Math.sqrt(d) / radius;
|
|
||||||
g = Math.floor(hue);
|
|
||||||
f = hue - g;
|
|
||||||
u = 255 * (1 - sat);
|
|
||||||
v = 255 * (1 - sat * f);
|
|
||||||
w = 255 * (1 - sat * (1 - f));
|
|
||||||
pixels[i] = [255, v, u, u, w, 255, 255][g];
|
|
||||||
pixels[i + 1] = [w, 255, 255, v, u, u, w][g];
|
|
||||||
pixels[i + 2] = [u, u, w, 255, 255, v, u][g];
|
|
||||||
pixels[i + 3] = 255;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
context.putImageData(imageData, 0, 0);
|
|
||||||
document.body.style.backgroundColor = 'white';
|
|
||||||
document.body.style.margin = '0px';
|
|
||||||
}
|
|
@ -1,36 +0,0 @@
|
|||||||
/**
|
|
||||||
* Copyright 2017 Google Inc. All rights reserved.
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
|
|
||||||
const Browser = require('../lib/Browser');
|
|
||||||
const Downloader = require('../utils/ChromiumDownloader');
|
|
||||||
|
|
||||||
let revision = '483012';
|
|
||||||
console.log('Downloading custom chromium revision - ' + revision);
|
|
||||||
Downloader.downloadRevision(Downloader.currentPlatform(), revision).then(async() => {
|
|
||||||
console.log('Done.');
|
|
||||||
let executablePath = Downloader.revisionInfo(Downloader.currentPlatform(), revision).executablePath;
|
|
||||||
let browser1 = new Browser({ executablePath });
|
|
||||||
let browser2 = new Browser();
|
|
||||||
let [version1, version2] = await Promise.all([
|
|
||||||
browser1.version(),
|
|
||||||
browser2.version()
|
|
||||||
]);
|
|
||||||
console.log('browser1: ' + version1);
|
|
||||||
console.log('browser2: ' + version2);
|
|
||||||
browser1.close();
|
|
||||||
browser2.close();
|
|
||||||
});
|
|
||||||
|
|
43
examples/detect-sniff.js
Normal file
43
examples/detect-sniff.js
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
/**
|
||||||
|
* Copyright 2017 Google Inc., PhantomJS Authors All rights reserved.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
(async() => {
|
||||||
|
|
||||||
|
const {Browser} = require('puppeteer');
|
||||||
|
const browser = new Browser();
|
||||||
|
|
||||||
|
const page = await browser.newPage();
|
||||||
|
await page.evaluateOnNewDocument(sniffDetector);
|
||||||
|
await page.navigate('https://www.google.com', {waitUntil: 'networkidle'});
|
||||||
|
console.log('Sniffed: ' + (await page.evaluate(() => !!navigator.sniffed)));
|
||||||
|
browser.close();
|
||||||
|
|
||||||
|
})();
|
||||||
|
|
||||||
|
function sniffDetector() {
|
||||||
|
let userAgent = window.navigator.userAgent;
|
||||||
|
let platform = window.navigator.platform;
|
||||||
|
|
||||||
|
window.navigator.__defineGetter__('userAgent', function() {
|
||||||
|
window.navigator.sniffed = true;
|
||||||
|
return userAgent;
|
||||||
|
});
|
||||||
|
|
||||||
|
window.navigator.__defineGetter__('platform', function() {
|
||||||
|
window.navigator.sniffed = true;
|
||||||
|
return platform;
|
||||||
|
});
|
||||||
|
}
|
@ -1,67 +0,0 @@
|
|||||||
/**
|
|
||||||
* Copyright 2017 Google Inc., PhantomJS Authors All rights reserved.
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
|
|
||||||
const Browser = require('../lib/Browser');
|
|
||||||
|
|
||||||
if (process.argv.length < 3) {
|
|
||||||
console.log('Usage: detectsniff.js <some URL>');
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
let address = process.argv[2];
|
|
||||||
console.log('Checking ' + address + '...');
|
|
||||||
|
|
||||||
let browser = new Browser();
|
|
||||||
browser.newPage().then(async page => {
|
|
||||||
await page.evaluateOnNewDocument(function() {
|
|
||||||
(function() {
|
|
||||||
let userAgent = window.navigator.userAgent,
|
|
||||||
platform = window.navigator.platform;
|
|
||||||
|
|
||||||
window.navigator = {
|
|
||||||
appCodeName: 'Mozilla',
|
|
||||||
appName: 'Netscape',
|
|
||||||
cookieEnabled: false,
|
|
||||||
sniffed: false
|
|
||||||
};
|
|
||||||
|
|
||||||
window.navigator.__defineGetter__('userAgent', function() {
|
|
||||||
window.navigator.sniffed = true;
|
|
||||||
return userAgent;
|
|
||||||
});
|
|
||||||
|
|
||||||
window.navigator.__defineGetter__('platform', function() {
|
|
||||||
window.navigator.sniffed = true;
|
|
||||||
return platform;
|
|
||||||
});
|
|
||||||
})();
|
|
||||||
});
|
|
||||||
let success = await page.navigate(address);
|
|
||||||
if (!success) {
|
|
||||||
console.log('FAIL to load the address');
|
|
||||||
browser.close();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
setTimeout(async function() {
|
|
||||||
let sniffed = await page.evaluate(() => navigator.sniffed);
|
|
||||||
if (sniffed)
|
|
||||||
console.log('The page tried to sniff the user agent.');
|
|
||||||
else
|
|
||||||
console.log('The page did not try to sniff the user agent.');
|
|
||||||
|
|
||||||
browser.close();
|
|
||||||
}, 1500);
|
|
||||||
});
|
|
@ -1,56 +0,0 @@
|
|||||||
/**
|
|
||||||
* Copyright 2017 Google Inc., PhantomJS Authors All rights reserved.
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
|
|
||||||
const path = require('path');
|
|
||||||
const Browser = require('../lib/Browser');
|
|
||||||
let browser = new Browser();
|
|
||||||
|
|
||||||
browser.newPage().then(async page => {
|
|
||||||
let modernizrPath = path.join(__dirname, '../third_party/phantomjs/examples/modernizr.js');
|
|
||||||
await page.injectFile(modernizrPath);
|
|
||||||
page.on('console', console.log);
|
|
||||||
await page.evaluate(detectFeatures);
|
|
||||||
browser.close();
|
|
||||||
});
|
|
||||||
|
|
||||||
function detectFeatures() {
|
|
||||||
let supported = [], unsupported = [];
|
|
||||||
console.log('Detected features (using Modernizr ' + Modernizr._version + '):');
|
|
||||||
for (let feature in Modernizr) {
|
|
||||||
if (Modernizr.hasOwnProperty(feature)) {
|
|
||||||
if (feature[0] !== '_' && typeof Modernizr[feature] !== 'function' &&
|
|
||||||
feature !== 'input' && feature !== 'inputtypes') {
|
|
||||||
if (Modernizr[feature])
|
|
||||||
supported.push(feature);
|
|
||||||
else
|
|
||||||
unsupported.push(feature);
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
console.log('');
|
|
||||||
console.log('Supported:');
|
|
||||||
supported.forEach(function(e) {
|
|
||||||
console.log(' ' + e);
|
|
||||||
});
|
|
||||||
|
|
||||||
console.log('');
|
|
||||||
console.log('Not supported:');
|
|
||||||
unsupported.forEach(function(e) {
|
|
||||||
console.log(' ' + e);
|
|
||||||
});
|
|
||||||
}
|
|
@ -1,40 +0,0 @@
|
|||||||
/**
|
|
||||||
* Copyright 2017 Google Inc., PhantomJS Authors All rights reserved.
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
|
|
||||||
const Browser = require('../lib/Browser');
|
|
||||||
|
|
||||||
if (process.argv.length < 5) {
|
|
||||||
console.log('Usage: openurlwithproxy.js <proxyHost> <proxyPort> <URL>');
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
let host = process.argv[2];
|
|
||||||
let port = process.argv[3];
|
|
||||||
let address = process.argv[4];
|
|
||||||
|
|
||||||
let browser = new Browser({
|
|
||||||
args: [ `--proxy-server=${host}:${port}`]
|
|
||||||
});
|
|
||||||
browser.newPage().then(async page => {
|
|
||||||
let success = await page.navigate(address);
|
|
||||||
if (success) {
|
|
||||||
console.log('Page title is ' + (await page.title()));
|
|
||||||
} else {
|
|
||||||
console.log('FAIL to load the address "' +
|
|
||||||
address + '" using proxy "' + host + ':' + port + '"');
|
|
||||||
}
|
|
||||||
browser.close();
|
|
||||||
});
|
|
@ -1,40 +0,0 @@
|
|||||||
/**
|
|
||||||
* Copyright 2017 Google Inc., PhantomJS Authors All rights reserved.
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
|
|
||||||
const Browser = require('../lib/Browser');
|
|
||||||
let browser = new Browser();
|
|
||||||
|
|
||||||
browser.newPage().then(async page => {
|
|
||||||
page.on('console', console.log);
|
|
||||||
|
|
||||||
|
|
||||||
await page.setInPageCallback('callPhantom', msg => {
|
|
||||||
console.log("Page is saying: '" + msg + "'");
|
|
||||||
return 'Hello, page';
|
|
||||||
});
|
|
||||||
|
|
||||||
|
|
||||||
await page.evaluate(async function() {
|
|
||||||
|
|
||||||
|
|
||||||
// Return-value of the "onCallback" handler arrive here
|
|
||||||
let callbackResponse = await window.callPhantom('Hello, driver');
|
|
||||||
console.log("Driver is saying: '" + callbackResponse + "'");
|
|
||||||
|
|
||||||
|
|
||||||
});
|
|
||||||
browser.close();
|
|
||||||
});
|
|
@ -14,7 +14,7 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
const Browser = require('../lib/Browser');
|
const {Browser} = require('puppeteer');
|
||||||
const browser = new Browser();
|
const browser = new Browser();
|
||||||
|
|
||||||
browser.newPage().then(async page => {
|
browser.newPage().then(async page => {
|
||||||
|
Loading…
Reference in New Issue
Block a user