From 48345eceb5260b62ee1bda1f5c21f586fdfe29b9 Mon Sep 17 00:00:00 2001 From: Jia Hao Date: Tue, 19 Jan 2016 11:30:42 +0800 Subject: [PATCH] Implement setting of user agent to fix #8 --- README.md | 24 +++++++++++++++++------- app/main.js | 5 +++-- src/buildApp.js | 12 +++++++----- src/cli.js | 5 ++++- src/options.js | 7 +++++-- 5 files changed, 36 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 83ef4e7..34f7e02 100644 --- a/README.md +++ b/README.md @@ -53,7 +53,7 @@ Specifies the destination directory to build the app to, defaults to the current Prints the usage information. -#### [App Name] +#### [app-name] ``` -n, --app-name @@ -68,15 +68,15 @@ The name of the application, which will affect strings in titles and the icon. ``` Automatically determined based on the current OS. Can be overwritten by specifying either `linux`, `win32`, or `darwin`. -#### [architecture] +#### [arch] ``` -a, --arch ``` -Automatically determined based on the current OS. Can be overwritten by specifying either `ia32` or `x64`. +Processor architecture, automatically determined based on the current OS. Can be overwritten by specifying either `ia32` or `x64`. -#### [Electron Version] +#### [electron-version] ``` -e, --electron-version @@ -85,7 +85,7 @@ Automatically determined based on the current OS. Can be overwritten by specifyi Electron version without the `v`, see https://github.com/atom/electron/releases. -#### [Overwrite] +#### [overwrite] ``` -o, --overwrite @@ -93,7 +93,7 @@ Electron version without the `v`, see https://github.com/atom/electron/releases. Specifies if the destination directory should be overwritten. -#### [Conceal] +#### [conceal] ``` -c, --conceal @@ -101,7 +101,7 @@ Specifies if the destination directory should be overwritten. Specifies if the source code within the nativefied app should be packaged into an archive, defaults to false, [read more](http://electron.atom.io/docs/v0.36.0/tutorial/application-packaging/). -#### [Icon] +#### [icon] ``` -i, --icon @@ -130,6 +130,7 @@ However, this would cause issues when the command line argument `target` is set ``` Width of the packaged application, defaults to `1280px`. + #### [height] ``` @@ -138,6 +139,15 @@ Width of the packaged application, defaults to `1280px`. Height of the packaged application, defaults to `800px`. +#### [user-agent] + +``` +-u, --user-agent +``` + +Set the user agent to run the created app with. + + ## 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/app/main.js b/app/main.js index 1932215..811072f 100644 --- a/app/main.js +++ b/app/main.js @@ -33,11 +33,12 @@ app.on('ready', function() { } } ); - mainWindow.loadUrl('file://' + __dirname + '/index.html'); + + mainWindow.loadUrl('file://' + __dirname + '/index.html', { userAgent: appArgs.userAgent}); //mainWindow.openDevTools(); - mainWindow.webContents.on('did-finish-load', function() { + mainWindow.webContents.on('did-finish-load', function() { mainWindow.webContents.send('params', JSON.stringify(appArgs)); }); diff --git a/src/buildApp.js b/src/buildApp.js index afcb417..04cf828 100644 --- a/src/buildApp.js +++ b/src/buildApp.js @@ -27,7 +27,7 @@ function buildApp(options, callback) { async.waterfall([ callback => { - copyPlaceholderApp(options.dir, tmpPath, options.name, options.targetUrl, options.badge, options.width, options.height, callback); + copyPlaceholderApp(options.dir, tmpPath, options.name, options.targetUrl, options.badge, options.width, options.height, options.userAgent, callback); }, (tempDir, callback) => { @@ -58,11 +58,12 @@ function buildApp(options, callback) { * @param {string} name * @param {string} targetURL * @param {boolean} badge - * @param {number} [width] - * @param {number} [height] + * @param {number} width + * @param {number} height + * @param {string} userAgent * @param {tempDirCallback} callback */ -function copyPlaceholderApp(srcAppDir, tempDir, name, targetURL, badge, width, height, callback) { +function copyPlaceholderApp(srcAppDir, tempDir, name, targetURL, badge, width, height, userAgent, callback) { copy(srcAppDir, tempDir, error => { if (error) { @@ -76,7 +77,8 @@ function copyPlaceholderApp(srcAppDir, tempDir, name, targetURL, badge, width, h targetUrl: targetURL, badge: badge, width: width, - height: height + height: height, + userAgent: userAgent }; fs.writeFileSync(path.join(tempDir, '/targetUrl.txt'), JSON.stringify(appArgs)); diff --git a/src/cli.js b/src/cli.js index 5d766a9..10d286d 100755 --- a/src/cli.js +++ b/src/cli.js @@ -25,7 +25,9 @@ function main(program) { program.icon, program.badge, program.width, - program.height, callback); + program.height, + program.userAgent, + callback); }, (options, callback) => { @@ -59,6 +61,7 @@ if (require.main === module) { .option('-i, --icon ', 'the icon file to use as the icon for the app (should be a .icns file on OSX)') .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') .parse(process.argv); if (!process.argv.slice(2).length) { diff --git a/src/options.js b/src/options.js index a36db8c..06efb24 100644 --- a/src/options.js +++ b/src/options.js @@ -20,7 +20,9 @@ function optionsFactory(name, icon, badge = false, width = 1280, - height = 800, callback) { + height = 800, + userAgent, + callback) { if (!validator.isURL(targetUrl, {require_protocol: true})) { throw `Your Url ${targetUrl} is invalid!, did you remember to include 'http://'?`; @@ -54,7 +56,8 @@ function optionsFactory(name, // app configuration badge: badge, width: width, - height: height + height: height, + userAgent: userAgent }; if (name && name.length > 0) {