2017-07-20 23:43:54 +00:00
|
|
|
#!/usr/bin/env node
|
|
|
|
/**
|
|
|
|
* 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 util = require('util');
|
|
|
|
const fs = require('fs');
|
|
|
|
const path = require('path');
|
2018-06-14 20:55:09 +00:00
|
|
|
const puppeteer = require('..');
|
2020-05-07 10:54:55 +00:00
|
|
|
const DEVICES_URL =
|
2021-09-14 15:02:39 +00:00
|
|
|
'https://raw.githubusercontent.com/ChromeDevTools/devtools-frontend/HEAD/front_end/emulated_devices/module.json';
|
2017-07-20 23:43:54 +00:00
|
|
|
|
|
|
|
const template = `/**
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
module.exports = %s;
|
2018-06-14 20:55:09 +00:00
|
|
|
for (const device of module.exports)
|
2017-07-20 23:43:54 +00:00
|
|
|
module.exports[device.name] = device;
|
|
|
|
`;
|
|
|
|
|
|
|
|
const help = `Usage: node ${path.basename(__filename)} [-u <from>] <outputPath>
|
|
|
|
-u, --url The URL to load devices descriptor from. If not set,
|
|
|
|
devices will be fetched from the tip-of-tree of DevTools
|
|
|
|
frontend.
|
|
|
|
|
|
|
|
-h, --help Show this help message
|
|
|
|
|
|
|
|
Fetch Chrome DevTools front-end emulation devices from given URL, convert them to puppeteer
|
|
|
|
devices and save to the <outputPath>.
|
|
|
|
`;
|
|
|
|
|
2017-08-21 23:39:04 +00:00
|
|
|
const argv = require('minimist')(process.argv.slice(2), {
|
2022-06-22 13:25:44 +00:00
|
|
|
alias: {u: 'url', h: 'help'},
|
2017-07-20 23:43:54 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
if (argv.help) {
|
|
|
|
console.log(help);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-08-21 23:39:04 +00:00
|
|
|
const url = argv.url || DEVICES_URL;
|
|
|
|
const outputPath = argv._[0];
|
2017-07-20 23:43:54 +00:00
|
|
|
if (!outputPath) {
|
|
|
|
console.log('ERROR: output file name is missing. Use --help for help.');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
main(url);
|
|
|
|
|
|
|
|
async function main(url) {
|
2018-06-14 20:55:09 +00:00
|
|
|
const browser = await puppeteer.launch();
|
|
|
|
const chromeVersion = (await browser.version()).split('/').pop();
|
|
|
|
await browser.close();
|
2017-07-20 23:43:54 +00:00
|
|
|
console.log('GET ' + url);
|
2017-08-21 23:39:04 +00:00
|
|
|
const text = await httpGET(url);
|
2017-07-20 23:43:54 +00:00
|
|
|
let json = null;
|
|
|
|
try {
|
|
|
|
json = JSON.parse(text);
|
2020-04-28 13:16:28 +00:00
|
|
|
} catch (error) {
|
|
|
|
console.error(`FAILED: error parsing response - ${error.message}`);
|
2017-07-20 23:43:54 +00:00
|
|
|
return;
|
|
|
|
}
|
2020-05-07 10:54:55 +00:00
|
|
|
const devicePayloads = json.extensions
|
2022-06-22 13:25:44 +00:00
|
|
|
.filter(extension => {
|
2022-06-15 10:42:21 +00:00
|
|
|
return extension.type === 'emulated-device';
|
|
|
|
})
|
2022-06-22 13:25:44 +00:00
|
|
|
.map(extension => {
|
2022-06-15 10:42:21 +00:00
|
|
|
return extension.device;
|
|
|
|
});
|
2017-07-20 23:43:54 +00:00
|
|
|
let devices = [];
|
2017-08-21 23:39:04 +00:00
|
|
|
for (const payload of devicePayloads) {
|
2018-06-14 20:55:09 +00:00
|
|
|
let names = [];
|
2022-06-14 11:55:35 +00:00
|
|
|
if (payload.title === 'iPhone 6/7/8') {
|
2018-06-14 20:55:09 +00:00
|
|
|
names = ['iPhone 6', 'iPhone 7', 'iPhone 8'];
|
2022-06-14 11:55:35 +00:00
|
|
|
} else if (payload.title === 'iPhone 6/7/8 Plus') {
|
2018-06-14 20:55:09 +00:00
|
|
|
names = ['iPhone 6 Plus', 'iPhone 7 Plus', 'iPhone 8 Plus'];
|
2022-06-14 11:55:35 +00:00
|
|
|
} else if (payload.title === 'iPhone 5/SE') {
|
|
|
|
names = ['iPhone 5', 'iPhone SE'];
|
|
|
|
} else {
|
|
|
|
names = [payload.title];
|
|
|
|
}
|
2018-06-14 20:55:09 +00:00
|
|
|
for (const name of names) {
|
|
|
|
const device = createDevice(chromeVersion, name, payload, false);
|
|
|
|
const landscape = createDevice(chromeVersion, name, payload, true);
|
|
|
|
devices.push(device);
|
2020-05-07 10:54:55 +00:00
|
|
|
if (
|
|
|
|
landscape.viewport.width !== device.viewport.width ||
|
|
|
|
landscape.viewport.height !== device.viewport.height
|
2022-06-14 11:55:35 +00:00
|
|
|
) {
|
2018-06-14 20:55:09 +00:00
|
|
|
devices.push(landscape);
|
2022-06-14 11:55:35 +00:00
|
|
|
}
|
2018-06-14 20:55:09 +00:00
|
|
|
}
|
2017-07-20 23:43:54 +00:00
|
|
|
}
|
2022-06-22 13:25:44 +00:00
|
|
|
devices = devices.filter(device => {
|
2022-06-15 10:42:21 +00:00
|
|
|
return device.viewport.isMobile;
|
|
|
|
});
|
|
|
|
devices.sort((a, b) => {
|
|
|
|
return a.name.localeCompare(b.name);
|
|
|
|
});
|
2017-07-20 23:43:54 +00:00
|
|
|
// Use single-quotes instead of double-quotes to conform with codestyle.
|
2017-08-21 23:39:04 +00:00
|
|
|
const serialized = JSON.stringify(devices, null, 2)
|
2020-05-07 10:54:55 +00:00
|
|
|
.replace(/'/g, `\\'`)
|
|
|
|
.replace(/"/g, `'`);
|
2017-08-21 23:39:04 +00:00
|
|
|
const result = util.format(template, serialized);
|
2017-07-20 23:43:54 +00:00
|
|
|
fs.writeFileSync(outputPath, result, 'utf8');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-06-14 20:55:09 +00:00
|
|
|
* @param {string} chromeVersion
|
|
|
|
* @param {string} deviceName
|
2017-07-20 23:43:54 +00:00
|
|
|
* @param {*} descriptor
|
|
|
|
* @param {boolean} landscape
|
2020-06-12 10:38:24 +00:00
|
|
|
* @returns {!Object}
|
2017-07-20 23:43:54 +00:00
|
|
|
*/
|
2018-06-14 20:55:09 +00:00
|
|
|
function createDevice(chromeVersion, deviceName, descriptor, landscape) {
|
2017-07-20 23:43:54 +00:00
|
|
|
const devicePayload = loadFromJSONV1(descriptor);
|
2020-05-07 10:54:55 +00:00
|
|
|
const viewportPayload = landscape
|
|
|
|
? devicePayload.horizontal
|
|
|
|
: devicePayload.vertical;
|
2017-07-20 23:43:54 +00:00
|
|
|
return {
|
2018-06-14 20:55:09 +00:00
|
|
|
name: deviceName + (landscape ? ' landscape' : ''),
|
2020-05-07 10:54:55 +00:00
|
|
|
userAgent: devicePayload.userAgent.includes('%s')
|
|
|
|
? util.format(devicePayload.userAgent, chromeVersion)
|
|
|
|
: devicePayload.userAgent,
|
2017-07-20 23:43:54 +00:00
|
|
|
viewport: {
|
|
|
|
width: viewportPayload.width,
|
|
|
|
height: viewportPayload.height,
|
|
|
|
deviceScaleFactor: devicePayload.deviceScaleFactor,
|
|
|
|
isMobile: devicePayload.capabilities.includes('mobile'),
|
|
|
|
hasTouch: devicePayload.capabilities.includes('touch'),
|
2020-05-07 10:54:55 +00:00
|
|
|
isLandscape: landscape || false,
|
|
|
|
},
|
2017-07-20 23:43:54 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {*} json
|
2020-06-12 10:38:24 +00:00
|
|
|
* @returns {?Object}
|
2017-07-20 23:43:54 +00:00
|
|
|
*/
|
|
|
|
function loadFromJSONV1(json) {
|
|
|
|
/**
|
|
|
|
* @param {*} object
|
|
|
|
* @param {string} key
|
|
|
|
* @param {string} type
|
|
|
|
* @param {*=} defaultValue
|
2020-06-12 10:38:24 +00:00
|
|
|
* @returns {*}
|
2017-07-20 23:43:54 +00:00
|
|
|
*/
|
|
|
|
function parseValue(object, key, type, defaultValue) {
|
2020-05-07 10:54:55 +00:00
|
|
|
if (
|
|
|
|
typeof object !== 'object' ||
|
|
|
|
object === null ||
|
|
|
|
!object.hasOwnProperty(key)
|
|
|
|
) {
|
2022-06-14 11:55:35 +00:00
|
|
|
if (typeof defaultValue !== 'undefined') {
|
|
|
|
return defaultValue;
|
|
|
|
}
|
2020-05-07 10:54:55 +00:00
|
|
|
throw new Error(
|
|
|
|
"Emulated device is missing required property '" + key + "'"
|
|
|
|
);
|
2017-07-20 23:43:54 +00:00
|
|
|
}
|
|
|
|
const value = object[key];
|
2022-06-14 11:55:35 +00:00
|
|
|
if (typeof value !== type || value === null) {
|
2020-05-07 10:54:55 +00:00
|
|
|
throw new Error(
|
|
|
|
"Emulated device property '" +
|
|
|
|
key +
|
|
|
|
"' has wrong type '" +
|
|
|
|
typeof value +
|
|
|
|
"'"
|
|
|
|
);
|
2022-06-14 11:55:35 +00:00
|
|
|
}
|
2017-07-20 23:43:54 +00:00
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {*} object
|
|
|
|
* @param {string} key
|
2020-06-12 10:38:24 +00:00
|
|
|
* @returns {number}
|
2017-07-20 23:43:54 +00:00
|
|
|
*/
|
|
|
|
function parseIntValue(object, key) {
|
|
|
|
const value = /** @type {number} */ (parseValue(object, key, 'number'));
|
2022-06-14 11:55:35 +00:00
|
|
|
if (value !== Math.abs(value)) {
|
2020-05-07 10:54:55 +00:00
|
|
|
throw new Error("Emulated device value '" + key + "' must be integer");
|
2022-06-14 11:55:35 +00:00
|
|
|
}
|
2017-07-20 23:43:54 +00:00
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {*} json
|
2020-06-12 10:38:24 +00:00
|
|
|
* @returns {!{width: number, height: number}}
|
2017-07-20 23:43:54 +00:00
|
|
|
*/
|
|
|
|
function parseOrientation(json) {
|
|
|
|
const result = {};
|
|
|
|
const minDeviceSize = 50;
|
|
|
|
const maxDeviceSize = 9999;
|
|
|
|
result.width = parseIntValue(json, 'width');
|
2020-05-07 10:54:55 +00:00
|
|
|
if (
|
|
|
|
result.width < 0 ||
|
|
|
|
result.width > maxDeviceSize ||
|
|
|
|
result.width < minDeviceSize
|
2022-06-14 11:55:35 +00:00
|
|
|
) {
|
2017-07-20 23:43:54 +00:00
|
|
|
throw new Error('Emulated device has wrong width: ' + result.width);
|
2022-06-14 11:55:35 +00:00
|
|
|
}
|
2017-07-20 23:43:54 +00:00
|
|
|
|
|
|
|
result.height = parseIntValue(json, 'height');
|
2020-05-07 10:54:55 +00:00
|
|
|
if (
|
|
|
|
result.height < 0 ||
|
|
|
|
result.height > maxDeviceSize ||
|
|
|
|
result.height < minDeviceSize
|
2022-06-14 11:55:35 +00:00
|
|
|
) {
|
2017-07-20 23:43:54 +00:00
|
|
|
throw new Error('Emulated device has wrong height: ' + result.height);
|
2022-06-14 11:55:35 +00:00
|
|
|
}
|
2017-07-20 23:43:54 +00:00
|
|
|
|
|
|
|
return /** @type {!{width: number, height: number}} */ (result);
|
|
|
|
}
|
|
|
|
|
|
|
|
const result = {};
|
|
|
|
result.type = /** @type {string} */ (parseValue(json, 'type', 'string'));
|
2021-05-12 14:48:30 +00:00
|
|
|
result.userAgent = /** @type {string} */ (
|
|
|
|
parseValue(json, 'user-agent', 'string')
|
|
|
|
);
|
2017-07-20 23:43:54 +00:00
|
|
|
|
|
|
|
const capabilities = parseValue(json, 'capabilities', 'object', []);
|
2022-06-14 11:55:35 +00:00
|
|
|
if (!Array.isArray(capabilities)) {
|
2017-07-20 23:43:54 +00:00
|
|
|
throw new Error('Emulated device capabilities must be an array');
|
2022-06-14 11:55:35 +00:00
|
|
|
}
|
2017-07-20 23:43:54 +00:00
|
|
|
result.capabilities = [];
|
|
|
|
for (let i = 0; i < capabilities.length; ++i) {
|
2022-06-14 11:55:35 +00:00
|
|
|
if (typeof capabilities[i] !== 'string') {
|
2017-07-20 23:43:54 +00:00
|
|
|
throw new Error('Emulated device capability must be a string');
|
2022-06-14 11:55:35 +00:00
|
|
|
}
|
2017-07-20 23:43:54 +00:00
|
|
|
result.capabilities.push(capabilities[i]);
|
|
|
|
}
|
|
|
|
|
2021-05-12 14:48:30 +00:00
|
|
|
result.deviceScaleFactor = /** @type {number} */ (
|
|
|
|
parseValue(json['screen'], 'device-pixel-ratio', 'number')
|
|
|
|
);
|
2022-06-14 11:55:35 +00:00
|
|
|
if (result.deviceScaleFactor < 0 || result.deviceScaleFactor > 100) {
|
2020-05-07 10:54:55 +00:00
|
|
|
throw new Error(
|
|
|
|
'Emulated device has wrong deviceScaleFactor: ' + result.deviceScaleFactor
|
|
|
|
);
|
2022-06-14 11:55:35 +00:00
|
|
|
}
|
2017-07-20 23:43:54 +00:00
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
result.vertical = parseOrientation(
|
|
|
|
parseValue(json['screen'], 'vertical', 'object')
|
|
|
|
);
|
|
|
|
result.horizontal = parseOrientation(
|
|
|
|
parseValue(json['screen'], 'horizontal', 'object')
|
|
|
|
);
|
2017-07-20 23:43:54 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {url}
|
2020-06-12 10:38:24 +00:00
|
|
|
* @returns {!Promise}
|
2017-07-20 23:43:54 +00:00
|
|
|
*/
|
|
|
|
function httpGET(url) {
|
|
|
|
let fulfill, reject;
|
|
|
|
const promise = new Promise((res, rej) => {
|
|
|
|
fulfill = res;
|
|
|
|
reject = rej;
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
const driver = url.startsWith('https://')
|
|
|
|
? require('https')
|
|
|
|
: require('http');
|
2022-06-22 13:25:44 +00:00
|
|
|
const request = driver.get(url, response => {
|
2017-07-20 23:43:54 +00:00
|
|
|
let data = '';
|
|
|
|
response.setEncoding('utf8');
|
2022-06-22 13:25:44 +00:00
|
|
|
response.on('data', chunk => {
|
2022-06-15 10:42:21 +00:00
|
|
|
return (data += chunk);
|
|
|
|
});
|
|
|
|
response.on('end', () => {
|
|
|
|
return fulfill(data);
|
|
|
|
});
|
2017-07-20 23:43:54 +00:00
|
|
|
response.on('error', reject);
|
|
|
|
});
|
|
|
|
request.on('error', reject);
|
|
|
|
return promise;
|
|
|
|
}
|