Implement setting of user agent to fix #8

This commit is contained in:
Jia Hao 2016-01-19 11:30:42 +08:00
parent fb4a99a872
commit 48345eceb5
5 changed files with 36 additions and 17 deletions

View File

@ -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 <value>
@ -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 <value>
```
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 <value>
@ -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 <path>
@ -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 <value>
```
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).

View File

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

View File

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

View File

@ -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 <value>', 'the icon file to use as the icon for the app (should be a .icns file on OSX)')
.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')
.parse(process.argv);
if (!process.argv.slice(2).length) {

View File

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