Implement `--pretend` flag to easily simulate user agent strings, fixes #11

This commit is contained in:
Jia Hao 2016-01-21 12:53:12 +08:00
parent 14a4846c1a
commit 82317a08d3
3 changed files with 38 additions and 0 deletions

View File

@ -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).

View File

@ -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 <value>', 'set window width, defaults to 1280px', parseInt)
.option('-h, --height <value>', 'set window height, defaults to 800px', parseInt)
.option('-u, --user-agent <value>', '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) {

View File

@ -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;