Skip to content

Commit 7a07cab

Browse files
authored
Merge pull request #3 from jxmot/20210519-add_devices
Can select Puppeteer devices for snapshots
2 parents 683a9b6 + 2129ea5 commit 7a07cab

File tree

7 files changed

+222
-10
lines changed

7 files changed

+222
-10
lines changed

README.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,9 @@ module.exports = {
5959
{width:1440, height:900},
6060
// extras, some smaller sizes...
6161
{width:1100, height:900},
62-
{width:825, height:900}
62+
{width:825, height:900},
63+
// can also select from Puppeteer's devices
64+
{device:'Nexus 6P'}
6365
],
6466
// these are optional, and can be omitted or
6567
// commented out. The folders in imgpath must
@@ -99,6 +101,8 @@ They are in order of most to least popular according to the following sources -
99101
* <https://www.w3schools.com/browsers/browsers_display.asp>
100102
* <https://gs.statcounter.com/screen-resolution-stats/desktop/north-america>
101103

104+
**NOTE**: A device can be added to the `views[]` array with `{device:'device name here'}`.
105+
102106
### Run-time Results
103107

104108
* Creates an image for each entry in `views[]`
@@ -118,11 +122,13 @@ queuing: target = https://example.com/ name = example-1536x864
118122
queuing: target = https://example.com/ name = example-1440x900
119123
queuing: target = https://example.com/ name = example-1100x900
120124
queuing: target = https://example.com/ name = example-825x900
125+
queuing: target = https://example.com/ name = example-Nexus_6P
121126
snap shots are in the queue...
122127
saved - example-1024x768.png
123128
saved - example-1280x800.png
124129
saved - example-1440x900.png
125130
saved - example-825x900.png
131+
saved - example-Nexus_6P.png
126132
saved - example-1920x1080.png
127133
saved - example-1366x768.png
128134
saved - example-1536x864.png
@@ -135,5 +141,11 @@ For additional information about *logging* in this application see:
135141
* github: <https://github.com/jxmot/simple-text-log>
136142
* npmjs: <https://www.npmjs.com/package/simple-text-log>
137143

144+
## Get the Puppeteer Device List
145+
146+
The `listdevices.js` application will create a list of Puppeteer devices. Unfortunately their documentation does not contain one and they direct you to the TypeScript source file where the devices are contained. I wanted a handy device list so I created this small app.
147+
148+
After [Installation](#installation) you can the get list with `node listdevices.js`. A file named `devicelist.txt` will be created and the list is written to the console.
149+
138150
---
139151
<img src="http://webexperiment.info/extcounter/mdcount.php?id=webpage-snapshot-tool">

devicelist.txt

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
Blackberry PlayBook - 600 X 1024
2+
Blackberry PlayBook landscape - 1024 X 600
3+
BlackBerry Z30 - 360 X 640
4+
BlackBerry Z30 landscape - 640 X 360
5+
Galaxy Note 3 - 360 X 640
6+
Galaxy Note 3 landscape - 640 X 360
7+
Galaxy Note II - 360 X 640
8+
Galaxy Note II landscape - 640 X 360
9+
Galaxy S III - 360 X 640
10+
Galaxy S III landscape - 640 X 360
11+
Galaxy S5 - 360 X 640
12+
Galaxy S5 landscape - 640 X 360
13+
iPad - 768 X 1024
14+
iPad landscape - 1024 X 768
15+
iPad Mini - 768 X 1024
16+
iPad Mini landscape - 1024 X 768
17+
iPad Pro - 1024 X 1366
18+
iPad Pro landscape - 1366 X 1024
19+
iPhone 4 - 320 X 480
20+
iPhone 4 landscape - 480 X 320
21+
iPhone 5 - 320 X 568
22+
iPhone 5 landscape - 568 X 320
23+
iPhone 6 - 375 X 667
24+
iPhone 6 landscape - 667 X 375
25+
iPhone 6 Plus - 414 X 736
26+
iPhone 6 Plus landscape - 736 X 414
27+
iPhone 7 - 375 X 667
28+
iPhone 7 landscape - 667 X 375
29+
iPhone 7 Plus - 414 X 736
30+
iPhone 7 Plus landscape - 736 X 414
31+
iPhone 8 - 375 X 667
32+
iPhone 8 landscape - 667 X 375
33+
iPhone 8 Plus - 414 X 736
34+
iPhone 8 Plus landscape - 736 X 414
35+
iPhone SE - 320 X 568
36+
iPhone SE landscape - 568 X 320
37+
iPhone X - 375 X 812
38+
iPhone X landscape - 812 X 375
39+
iPhone XR - 414 X 896
40+
iPhone XR landscape - 896 X 414
41+
iPhone 11 - 414 X 828
42+
iPhone 11 landscape - 828 X 414
43+
iPhone 11 Pro - 375 X 812
44+
iPhone 11 Pro landscape - 812 X 375
45+
iPhone 11 Pro Max - 414 X 896
46+
iPhone 11 Pro Max landscape - 896 X 414
47+
JioPhone 2 - 240 X 320
48+
JioPhone 2 landscape - 320 X 240
49+
Kindle Fire HDX - 800 X 1280
50+
Kindle Fire HDX landscape - 1280 X 800
51+
LG Optimus L70 - 384 X 640
52+
LG Optimus L70 landscape - 640 X 384
53+
Microsoft Lumia 550 - 640 X 360
54+
Microsoft Lumia 950 - 360 X 640
55+
Microsoft Lumia 950 landscape - 640 X 360
56+
Nexus 10 - 800 X 1280
57+
Nexus 10 landscape - 1280 X 800
58+
Nexus 4 - 384 X 640
59+
Nexus 4 landscape - 640 X 384
60+
Nexus 5 - 360 X 640
61+
Nexus 5 landscape - 640 X 360
62+
Nexus 5X - 412 X 732
63+
Nexus 5X landscape - 732 X 412
64+
Nexus 6 - 412 X 732
65+
Nexus 6 landscape - 732 X 412
66+
Nexus 6P - 412 X 732
67+
Nexus 6P landscape - 732 X 412
68+
Nexus 7 - 600 X 960
69+
Nexus 7 landscape - 960 X 600
70+
Nokia Lumia 520 - 320 X 533
71+
Nokia Lumia 520 landscape - 533 X 320
72+
Nokia N9 - 480 X 854
73+
Nokia N9 landscape - 854 X 480
74+
Pixel 2 - 411 X 731
75+
Pixel 2 landscape - 731 X 411
76+
Pixel 2 XL - 411 X 823
77+
Pixel 2 XL landscape - 823 X 411

index.js

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -112,17 +112,36 @@ for(let idx = 0; idx < targetopt.views.length; idx++) {
112112
name = target.split('//')[1].split('/')[0];
113113
}
114114
}
115-
// add the viewport dimensions to the name
116-
name = name + '-' + targetopt.views[idx].width + 'x' + targetopt.views[idx].height;
117-
log(`queuing: target = ${target} snapshot file = ${imgpath}${name}${imgextn}`);
115+
116+
/*
117+
targetopt.views[] can contain either viewport dimensions:
118+
{width:950, height:1080}
119+
120+
OR device names:
121+
{device:'Nexus 6P'}
122+
*/
123+
if(targetopt.views[idx].width) {
124+
// add the viewport dimensions to the name
125+
name = name + '-' + targetopt.views[idx].width + 'x' + targetopt.views[idx].height;
126+
log(`queuing: target = ${target} snapshot file = ${imgpath}${name}${imgextn}`);
127+
}
128+
129+
if(targetopt.views[idx].device) {
130+
// add the device name to the file name
131+
name = name + '-' + targetopt.views[idx].device.replace(/ /g,'_');
132+
log(`queuing: target = ${target} snapshot file = ${imgpath}${name}${imgextn}`);
133+
}
118134

119135
(async () => {
120136
const browser = await puppeteer.launch({headless:true});
121137
const page = await browser.newPage();
122138
// ONLY has mobile emulation!!!
123-
// await page.emulate(puppeteer.devices['iPhone 6']);
124-
// the alternative is to provide our own views...
125-
await page.setViewport(targetopt.views[idx]);
139+
if(targetopt.views[idx].device) {
140+
await page.emulate(puppeteer.devices[targetopt.views[idx].device]);
141+
} else {
142+
// the alternative is to provide our own views...
143+
await page.setViewport(targetopt.views[idx]);
144+
}
126145
// get the page and wait for things to settle
127146
await page.goto(target,{waitUntil:'networkidle0'});
128147
// give time for page load and render

listdevices.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
'use strict';
2+
/*
3+
List all devices that Puppeteer contains:
4+
5+
node listdevices.js
6+
7+
Creates a text file listing all of the device names
8+
and their dimensions:
9+
10+
Nexus 6P - 412 X 732
11+
*/
12+
// set up run-time logging first...
13+
const Log = require('simple-text-log');
14+
const logOut = new Log({logfile:'./devicelist.txt',logsize:0});
15+
// optionally also write to the console
16+
const logcon = true;
17+
function log(payload) {
18+
if(logcon) console.log(payload);
19+
logOut.write(payload);
20+
};
21+
const puppeteer = require('puppeteer');
22+
Object.keys(puppeteer.devices).forEach((key, idx) => {
23+
let dev = puppeteer.devices[key];
24+
log(`${dev.name} - ${dev.viewport.width} X ${dev.viewport.height}`);
25+
});

puppeteer-device_list-20210517.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
'{
1+
{
22
"Blackberry PlayBook": {
33
"name": "Blackberry PlayBook",
44
"userAgent": "Mozilla/5.0 (PlayBook; U; RIM Tablet OS 2.1.0; en-US) AppleWebKit/536.2+ (KHTML like Gecko) Version/7.2.1.0 Safari/536.2+",
@@ -923,4 +923,4 @@
923923
"isLandscape": true
924924
}
925925
}
926-
}'
926+
}

puppeteer-device_list-20210517.md

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
Blackberry PlayBook - 600 X 1024
2+
Blackberry PlayBook landscape - 1024 X 600
3+
BlackBerry Z30 - 360 X 640
4+
BlackBerry Z30 landscape - 640 X 360
5+
Galaxy Note 3 - 360 X 640
6+
Galaxy Note 3 landscape - 640 X 360
7+
Galaxy Note II - 360 X 640
8+
Galaxy Note II landscape - 640 X 360
9+
Galaxy S III - 360 X 640
10+
Galaxy S III landscape - 640 X 360
11+
Galaxy S5 - 360 X 640
12+
Galaxy S5 landscape - 640 X 360
13+
iPad - 768 X 1024
14+
iPad landscape - 1024 X 768
15+
iPad Mini - 768 X 1024
16+
iPad Mini landscape - 1024 X 768
17+
iPad Pro - 1024 X 1366
18+
iPad Pro landscape - 1366 X 1024
19+
iPhone 4 - 320 X 480
20+
iPhone 4 landscape - 480 X 320
21+
iPhone 5 - 320 X 568
22+
iPhone 5 landscape - 568 X 320
23+
iPhone 6 - 375 X 667
24+
iPhone 6 landscape - 667 X 375
25+
iPhone 6 Plus - 414 X 736
26+
iPhone 6 Plus landscape - 736 X 414
27+
iPhone 7 - 375 X 667
28+
iPhone 7 landscape - 667 X 375
29+
iPhone 7 Plus - 414 X 736
30+
iPhone 7 Plus landscape - 736 X 414
31+
iPhone 8 - 375 X 667
32+
iPhone 8 landscape - 667 X 375
33+
iPhone 8 Plus - 414 X 736
34+
iPhone 8 Plus landscape - 736 X 414
35+
iPhone SE - 320 X 568
36+
iPhone SE landscape - 568 X 320
37+
iPhone X - 375 X 812
38+
iPhone X landscape - 812 X 375
39+
iPhone XR - 414 X 896
40+
iPhone XR landscape - 896 X 414
41+
iPhone 11 - 414 X 828
42+
iPhone 11 landscape - 828 X 414
43+
iPhone 11 Pro - 375 X 812
44+
iPhone 11 Pro landscape - 812 X 375
45+
iPhone 11 Pro Max - 414 X 896
46+
iPhone 11 Pro Max landscape - 896 X 414
47+
JioPhone 2 - 240 X 320
48+
JioPhone 2 landscape - 320 X 240
49+
Kindle Fire HDX - 800 X 1280
50+
Kindle Fire HDX landscape - 1280 X 800
51+
LG Optimus L70 - 384 X 640
52+
LG Optimus L70 landscape - 640 X 384
53+
Microsoft Lumia 550 - 640 X 360
54+
Microsoft Lumia 950 - 360 X 640
55+
Microsoft Lumia 950 landscape - 640 X 360
56+
Nexus 10 - 800 X 1280
57+
Nexus 10 landscape - 1280 X 800
58+
Nexus 4 - 384 X 640
59+
Nexus 4 landscape - 640 X 384
60+
Nexus 5 - 360 X 640
61+
Nexus 5 landscape - 640 X 360
62+
Nexus 5X - 412 X 732
63+
Nexus 5X landscape - 732 X 412
64+
Nexus 6 - 412 X 732
65+
Nexus 6 landscape - 732 X 412
66+
Nexus 6P - 412 X 732
67+
Nexus 6P landscape - 732 X 412
68+
Nexus 7 - 600 X 960
69+
Nexus 7 landscape - 960 X 600
70+
Nokia Lumia 520 - 320 X 533
71+
Nokia Lumia 520 landscape - 533 X 320
72+
Nokia N9 - 480 X 854
73+
Nokia N9 landscape - 854 X 480
74+
Pixel 2 - 411 X 731
75+
Pixel 2 landscape - 731 X 411
76+
Pixel 2 XL - 411 X 823
77+
Pixel 2 XL landscape - 823 X 411

target-example.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ module.exports = {
2222
{width:1440, height:900},
2323
// extras, some smaller sizes...
2424
{width:1100, height:900},
25-
{width:825, height:900}
25+
{width:825, height:900},
26+
// can also select from Puppeteer's devices
27+
{device:'Nexus 6P'}
2628
],
2729
// these are optional, and can be omitted or
2830
// commented out. The folders in imgpath must

0 commit comments

Comments
 (0)