From 82317a08d3daea893544c30ff5e1c4417c861bb2 Mon Sep 17 00:00:00 2001 From: Jia Hao Date: Thu, 21 Jan 2016 12:53:12 +0800 Subject: [PATCH] Implement `--pretend` flag to easily simulate user agent strings, fixes #11 --- README.md | 15 +++++++++++++++ src/cli.js | 2 ++ src/options.js | 21 +++++++++++++++++++++ 3 files changed, 38 insertions(+) diff --git a/README.md b/README.md index 8e45acf..69033ff 100644 --- a/README.md +++ b/README.md @@ -35,6 +35,14 @@ Nativefier will intelligently attempt to determine the app name, your OS and pro $ nativefier --app-name "Some Awesome App" "http://medium.com" ``` +Certain websites such as WhatsApp Web might say that your browser is unsupported. To get around this, simply pass in the `--pretend` flag, as such: + +``` +$ nativefier --pretend "http://web.whatsapp.com" +``` + +This will use a preset user agent string to make the website think you're accessing it from a regular Google Chrome browser. + Other command line options are listed below. ## Options @@ -152,6 +160,13 @@ Height of the packaged application, defaults to `800px`. Set the user agent to run the created app with. +### [pretend] +``` +-p, --pretend +``` + +Uses a preset user agent string for your OS and pretends to be a regular Google Chrome browser. + ## How It Works A template app with the appropriate event listeners and callbacks set up is included in the `./app` folder. When the `nativefier` command is executed, this folder is copied to a temporary directory with the appropriate parameters in a configuration file, and is packaged into an app with [Electron Packager](https://github.com/maxogden/electron-packager). diff --git a/src/cli.js b/src/cli.js index 10d286d..013d05f 100755 --- a/src/cli.js +++ b/src/cli.js @@ -27,6 +27,7 @@ function main(program) { program.width, program.height, program.userAgent, + program.pretend, callback); }, @@ -62,6 +63,7 @@ if (require.main === module) { .option('-w, --width ', 'set window width, defaults to 1280px', parseInt) .option('-h, --height ', 'set window height, defaults to 800px', parseInt) .option('-u, --user-agent ', 'set the user agent string for the app') + .option('-p, --pretend', 'pretends to be a normal chrome browser') .parse(process.argv); if (!process.argv.slice(2).length) { diff --git a/src/options.js b/src/options.js index b445870..d30e0d6 100644 --- a/src/options.js +++ b/src/options.js @@ -23,6 +23,7 @@ function optionsFactory(name, width = 1280, height = 800, userAgent, + pretend, callback) { targetUrl = normalizeUrl(targetUrl); @@ -35,6 +36,10 @@ function optionsFactory(name, height = 800; } + if (!userAgent && pretend) { + userAgent = getFakeUserAgent(); + } + const options = { dir: TEMPLATE_APP_DIR, @@ -121,4 +126,20 @@ function normalizeUrl(testUrl) { return normalized; } +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; + } + return userAgent; +} + export default optionsFactory;