2
2
mirror of https://github.com/Llewellynvdm/nativefier.git synced 2024-11-11 07:41:04 +00:00

Infer user agent from Electron's online version list

This commit is contained in:
sa 2016-03-19 21:14:41 -04:00
parent 266a239104
commit 4b6716efef
2 changed files with 72 additions and 20 deletions

View File

@ -0,0 +1,52 @@
import request from 'request';
import _ from 'lodash';
const ELECTRON_VERSIONS_URL = 'https://atom.io/download/atom-shell/index.json';
function getChromeVersionForElectronVersion(electronVersion, url = ELECTRON_VERSIONS_URL) {
return new Promise((resolve, reject) => {
request(url, (error, response, body) => {
if (error) {
reject(error);
return;
}
if (response.statusCode === 200) {
const data = JSON.parse(body);
const electronVersionToChromeVersion = _.zipObject(data.map(d => d.version), data.map(d => d.chrome));
if (!(electronVersion in electronVersionToChromeVersion)) {
reject(`Electron version '${ electronVersion }' not found in retrieved version list!`);
return;
}
resolve(electronVersionToChromeVersion[electronVersion]);
return;
}
reject('Bad request: ' + response.statusCode);
return;
});
});
}
export function getUserAgentString(chromeVersion, platform) {
let userAgent;
switch (platform) {
case 'darwin':
userAgent = `Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/${ chromeVersion } Safari/537.36`;
break;
case 'win32':
userAgent = `Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/${ chromeVersion } Safari/537.36`;
break;
case 'linux':
userAgent = `Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/${ chromeVersion } Safari/537.36`;
break;
default:
break;
}
return userAgent;
}
export function inferUserAgent(electronVersion, platform) {
return getChromeVersionForElectronVersion(electronVersion)
.then(chromeVersion => {
return getUserAgentString(chromeVersion, platform);
});
}

View File

@ -1,4 +1,3 @@
import os from 'os';
import path from 'path'; import path from 'path';
import _ from 'lodash'; import _ from 'lodash';
import async from 'async'; import async from 'async';
@ -7,6 +6,7 @@ import sanitizeFilenameLib from 'sanitize-filename';
import inferIcon from './../infer/inferIcon'; import inferIcon from './../infer/inferIcon';
import inferTitle from './../infer/inferTitle'; import inferTitle from './../infer/inferTitle';
import inferOs from './../infer/inferOs'; import inferOs from './../infer/inferOs';
import {inferUserAgent, getUserAgentString} from './../infer/inferUserAgent';
import normalizeUrl from './normalizeUrl'; import normalizeUrl from './normalizeUrl';
import packageJson from './../../package.json'; import packageJson from './../../package.json';
@ -14,6 +14,8 @@ const {inferPlatform, inferArch} = inferOs;
const PLACEHOLDER_APP_DIR = path.join(__dirname, '../../', 'app'); const PLACEHOLDER_APP_DIR = path.join(__dirname, '../../', 'app');
const ELECTRON_VERSION = '0.36.4'; const ELECTRON_VERSION = '0.36.4';
const CHROME_VERSION = '47.0.2526.73';
const DEFAULT_APP_NAME = 'APP'; const DEFAULT_APP_NAME = 'APP';
/** /**
@ -45,7 +47,7 @@ function optionsFactory(inpOptions, callback) {
width: inpOptions.width || 1280, width: inpOptions.width || 1280,
height: inpOptions.height || 800, height: inpOptions.height || 800,
showMenuBar: inpOptions.showMenuBar || false, showMenuBar: inpOptions.showMenuBar || false,
userAgent: inpOptions.userAgent || getFakeUserAgent(), userAgent: inpOptions.userAgent,
ignoreCertificate: inpOptions.ignoreCertificate || false, ignoreCertificate: inpOptions.ignoreCertificate || false,
insecure: inpOptions.insecure || false, insecure: inpOptions.insecure || false,
flashPluginDir: inpOptions.flash || null, flashPluginDir: inpOptions.flash || null,
@ -66,6 +68,22 @@ function optionsFactory(inpOptions, callback) {
} }
async.waterfall([ async.waterfall([
callback => {
if (options.userAgent) {
callback();
return;
}
inferUserAgent(options.version, options.platform)
.then(userAgent => {
options.userAgent = userAgent;
callback();
})
.catch(error => {
console.warn('Cannot get user agent:', error);
options.userAgent = getUserAgentString(CHROME_VERSION, options.platform);
callback();
});
},
callback => { callback => {
if (options.icon) { if (options.icon) {
callback(); callback();
@ -118,22 +136,4 @@ function sanitizeOptions(options) {
return options; return options;
} }
function getFakeUserAgent() {
let userAgent;
switch (os.platform()) {
case 'darwin':
userAgent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2227.1 Safari/537.36';
break;
case 'win32':
userAgent = 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36';
break;
case 'linux':
userAgent = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2227.0 Safari/537.36';
break;
default:
break;
}
return userAgent;
}
export default optionsFactory; export default optionsFactory;