From 29adc5dc80649ba062cf44c08391ae62a1a1d327 Mon Sep 17 00:00:00 2001 From: Andrey Lushnikov Date: Tue, 8 Aug 2017 00:44:15 -0700 Subject: [PATCH] [examples] get rid of old examples This patch: - removes old phantom-js examples - adds an example to load page without images References #178 --- README.md | 2 +- .../{loadurlwithoutcss.js => block-images.js} | 34 ++++----- examples/colorwheel.js | 71 ------------------- examples/custom-chromium-revision.js | 36 ---------- examples/detect-sniff.js | 43 +++++++++++ examples/detectsniff.js | 67 ----------------- examples/features.js | 56 --------------- examples/openurlwithproxy.js | 40 ----------- examples/pagecallback.js | 40 ----------- examples/screenshot.js | 2 +- 10 files changed, 60 insertions(+), 331 deletions(-) rename examples/{loadurlwithoutcss.js => block-images.js} (55%) delete mode 100644 examples/colorwheel.js delete mode 100644 examples/custom-chromium-revision.js create mode 100644 examples/detect-sniff.js delete mode 100644 examples/detectsniff.js delete mode 100644 examples/features.js delete mode 100644 examples/openurlwithproxy.js delete mode 100644 examples/pagecallback.js diff --git a/README.md b/README.md index d75fd06c5e6..76c84f992b7 100644 --- a/README.md +++ b/README.md @@ -23,7 +23,7 @@ To add Puppeteer to your project, run: 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 diff --git a/examples/loadurlwithoutcss.js b/examples/block-images.js similarity index 55% rename from examples/loadurlwithoutcss.js rename to examples/block-images.js index f4f1f42093a..53c9e87b317 100644 --- a/examples/loadurlwithoutcss.js +++ b/examples/block-images.js @@ -14,25 +14,21 @@ * limitations under the License. */ -const Browser = require('../lib/Browser'); +(async() => { -if (process.argv.length < 3) { - console.log('Usage: loadurlwithoutcss.js URL'); - return; -} +const {Browser} = require('puppeteer'); +const browser = new Browser(); -let address = process.argv[2]; - -let browser = new Browser({headless: false}); -browser.newPage().then(async page => { - page.setRequestInterceptor(request => { - if (request.url.endsWith('.css')) - request.abort(); - else - request.continue(); - }); - let success = await page.navigate(address); - if (!success) - console.log('Unable to load the address!'); - browser.close(); +const page = await browser.newPage(); +await page.setRequestInterceptor(request => { + if (/\.(png|jpg|jpeg$)/.test(request.url)) + request.abort(); + else + request.continue(); }); +await page.navigate('https://bbc.com'); +await page.screenshot({path: 'news.png', fullPage: true}); +browser.close(); + +})(); + diff --git a/examples/colorwheel.js b/examples/colorwheel.js deleted file mode 100644 index bbe3796bf04..00000000000 --- a/examples/colorwheel.js +++ /dev/null @@ -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(''); - 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'; -} diff --git a/examples/custom-chromium-revision.js b/examples/custom-chromium-revision.js deleted file mode 100644 index 30485747665..00000000000 --- a/examples/custom-chromium-revision.js +++ /dev/null @@ -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(); -}); - diff --git a/examples/detect-sniff.js b/examples/detect-sniff.js new file mode 100644 index 00000000000..f71c61a1ba3 --- /dev/null +++ b/examples/detect-sniff.js @@ -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; + }); +} diff --git a/examples/detectsniff.js b/examples/detectsniff.js deleted file mode 100644 index 1085454be00..00000000000 --- a/examples/detectsniff.js +++ /dev/null @@ -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 '); - 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); -}); diff --git a/examples/features.js b/examples/features.js deleted file mode 100644 index 4f9081202d3..00000000000 --- a/examples/features.js +++ /dev/null @@ -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); - }); -} diff --git a/examples/openurlwithproxy.js b/examples/openurlwithproxy.js deleted file mode 100644 index b12ad45428d..00000000000 --- a/examples/openurlwithproxy.js +++ /dev/null @@ -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 '); - 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(); -}); diff --git a/examples/pagecallback.js b/examples/pagecallback.js deleted file mode 100644 index 32edb778f0d..00000000000 --- a/examples/pagecallback.js +++ /dev/null @@ -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(); -}); diff --git a/examples/screenshot.js b/examples/screenshot.js index c9e72899733..0af68c98523 100644 --- a/examples/screenshot.js +++ b/examples/screenshot.js @@ -14,7 +14,7 @@ * limitations under the License. */ -const Browser = require('../lib/Browser'); +const {Browser} = require('puppeteer'); const browser = new Browser(); browser.newPage().then(async page => {