1 | /**
|
2 | Licensed to the Apache Software Foundation (ASF) under one
|
3 | or more contributor license agreements. See the NOTICE file
|
4 | distributed with this work for additional information
|
5 | regarding copyright ownership. The ASF licenses this file
|
6 | to you under the Apache License, Version 2.0 (the
|
7 | "License"); you may not use this file except in compliance
|
8 | with the License. You may obtain a copy of the License at
|
9 |
|
10 | http://www.apache.org/licenses/LICENSE-2.0
|
11 |
|
12 | Unless required by applicable law or agreed to in writing,
|
13 | software distributed under the License is distributed on an
|
14 | "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
15 | KIND, either express or implied. See the License for the
|
16 | specific language governing permissions and limitations
|
17 | under the License.
|
18 | */
|
19 |
|
20 | var exec = require('./exec'),
|
21 | Q = require('q');
|
22 |
|
23 | /**
|
24 | * Launches the specified browser with the given URL.
|
25 | * Based on https://github.com/domenic/opener
|
26 | * @param {string} target Browser target to launch.
|
27 | * @param {string} url
|
28 | * @return {Q} Promise to launch the specified browser
|
29 | */
|
30 | module.exports = function (target, url) {
|
31 | return mapTarget(target).then(function (browser) {
|
32 | var args;
|
33 | switch (process.platform) {
|
34 | case 'darwin':
|
35 | args = ['open'];
|
36 | if (target == 'chrome') {
|
37 | // Chrome needs to be launched in a new window. Other browsers, particularly, opera does not work with this.
|
38 | args.push('-n');
|
39 | }
|
40 | args.push('-a', browser);
|
41 | break;
|
42 | case 'win32':
|
43 | // On Windows, we really want to use the "start" command. But, the rules regarding arguments with spaces, and
|
44 | // escaping them with quotes, can get really arcane. So the easiest way to deal with this is to pass off the
|
45 | // responsibility to "cmd /c", which has that logic built in.
|
46 | //
|
47 | // Furthermore, if "cmd /c" double-quoted the first parameter, then "start" will interpret it as a window title,
|
48 | // so we need to add a dummy empty-string window title: http://stackoverflow.com/a/154090/3191
|
49 | args = ['cmd /c start ""', browser];
|
50 | break;
|
51 | case 'linux':
|
52 | // if a browser is specified, launch it with the url as argument
|
53 | // otherwise, use xdg-open.
|
54 | args = [browser];
|
55 | break;
|
56 | }
|
57 | args.push(url);
|
58 | var command = args.join(' ');
|
59 | console.log('Executing command: ' + command);
|
60 | return exec(command);
|
61 | });
|
62 | };
|
63 |
|
64 | function mapTarget(target) {
|
65 | target = target || 'chrome';
|
66 |
|
67 | var chromeArgs = ' --user-data-dir=/tmp/temp_chrome_user_data_dir_for_cordova_browser';
|
68 | var browsers = {
|
69 | 'win32': {
|
70 | 'ie': 'iexplore',
|
71 | 'chrome': 'chrome --user-data-dir=C:/Chromedevsession',
|
72 | 'safari': 'safari',
|
73 | 'opera': 'opera',
|
74 | 'firefox': 'firefox'
|
75 | },
|
76 | 'darwin': {
|
77 | 'chrome': '"Google Chrome" --args' + chromeArgs,
|
78 | 'safari': 'safari',
|
79 | 'firefox': 'firefox',
|
80 | 'opera': 'opera'
|
81 | },
|
82 | 'linux' : {
|
83 | 'chrome': 'google-chrome' + chromeArgs ,
|
84 | 'chromium': 'chromium-browser' + chromeArgs,
|
85 | 'firefox': 'firefox',
|
86 | 'opera': 'opera'
|
87 | }
|
88 | };
|
89 | target = target.toLowerCase();
|
90 | if (target in browsers[process.platform]) {
|
91 | return Q(browsers[process.platform][target]);
|
92 | }
|
93 | return Q.reject('Browser target not supported: ' + target);
|
94 | }
|