/
browser.js
238 lines (213 loc) · 8.75 KB
/
browser.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/**
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
*/
/* globals Promise: true */
var child_process = require('child_process');
var fs = require('fs');
var open = require('opn');
var exec = require('./exec');
var NOT_INSTALLED = 'The browser target is not installed: %target%';
var NOT_SUPPORTED = 'The browser target is not supported: %target%';
/**
* Launches the specified browser with the given URL.
* Based on https://github.com/domenic/opener
* @param {{target: ?string, url: ?string, dataDir: ?string}} opts - parameters:
* target - the target browser - ie, chrome, safari, opera, firefox or chromium
* url - the url to open in the browser
* dataDir - a data dir to provide to Chrome (can be used to force it to open in a new window)
* @return {Promise} Promise to launch the specified browser
*/
module.exports = function (opts) {
opts = opts || {};
var target = opts.target || 'default';
var url = opts.url || '';
target = target.toLowerCase();
if (target === 'default') {
open(url);
return Promise.resolve();
} else {
return getBrowser(target, opts.dataDir).then(function (browser) {
var args;
var urlAdded = false;
switch (process.platform) {
case 'darwin':
args = ['open'];
if (target === 'chrome') {
// Chrome needs to be launched in a new window. Other browsers, particularly, opera does not work with this.
args.push('-n');
}
args.push('-a', browser);
break;
case 'win32':
// On Windows, we really want to use the "start" command. But, the rules regarding arguments with spaces, and
// escaping them with quotes, can get really arcane. So the easiest way to deal with this is to pass off the
// responsibility to "cmd /c", which has that logic built in.
//
// Furthermore, if "cmd /c" double-quoted the first parameter, then "start" will interpret it as a window title,
// so we need to add a dummy empty-string window title: http://stackoverflow.com/a/154090/3191
if (target === 'edge') {
browser += ':' + url;
urlAdded = true;
}
args = ['cmd /c start ""', browser];
break;
case 'linux':
// if a browser is specified, launch it with the url as argument
// otherwise, use xdg-open.
args = [browser];
break;
}
if (!urlAdded) {
args.push(url);
}
var command = args.join(' ');
var result = exec(command);
result.catch(function () {
// Assume any error means that the browser is not installed and display that as a more friendly error.
throw new Error(NOT_INSTALLED.replace('%target%', target));
});
return result;
// return exec(command).catch(function (error) {
// // Assume any error means that the browser is not installed and display that as a more friendly error.
// throw new Error(NOT_INSTALLED.replace('%target%', target));
// });
});
}
};
function getBrowser (target, dataDir) {
dataDir = dataDir || 'temp_chrome_user_data_dir_for_cordova';
var chromeArgs = ' --user-data-dir=/tmp/' + dataDir;
var browsers = {
'win32': {
'ie': 'iexplore',
'chrome': 'chrome --user-data-dir=%TEMP%\\' + dataDir,
'safari': 'safari',
'opera': 'opera',
'firefox': 'firefox',
'edge': 'microsoft-edge'
},
'darwin': {
'chrome': '"Google Chrome" --args' + chromeArgs,
'safari': 'safari',
'firefox': 'firefox',
'opera': 'opera'
},
'linux': {
'chrome': 'google-chrome' + chromeArgs,
'chromium': 'chromium-browser' + chromeArgs,
'firefox': 'firefox',
'opera': 'opera'
}
};
if (target in browsers[process.platform]) {
var browser = browsers[process.platform][target];
return checkBrowserExistsWindows(browser, target).then(function () {
return Promise.resolve(browser);
});
} else {
return Promise.reject(NOT_SUPPORTED.replace('%target%', target));
}
}
// err might be null, in which case defaultMsg is used.
// target MUST be defined or an error is thrown.
function getErrorMessage (err, target, defaultMsg) {
var errMessage;
if (err) {
errMessage = err.toString();
} else {
errMessage = defaultMsg;
}
return errMessage.replace('%target%', target);
}
function checkBrowserExistsWindows (browser, target) {
var promise = new Promise(function (resolve, reject) {
// Windows displays a dialog if the browser is not installed. We'd prefer to avoid that.
if (process.platform === 'win32') {
if (target === 'edge') {
edgeSupported().then(function () {
resolve();
})
.catch(function (err) {
var errMessage = getErrorMessage(err, target, NOT_INSTALLED);
reject(errMessage);
});
} else {
browserInstalled(browser).then(function () {
resolve();
})
.catch(function (err) {
var errMessage = getErrorMessage(err, target, NOT_INSTALLED);
reject(errMessage);
});
}
} else {
resolve();
}
});
return promise;
}
function edgeSupported () {
var prom = new Promise(function (resolve, reject) {
child_process.exec('ver', function (err, stdout, stderr) {
if (err || stderr) {
reject(err || stderr);
} else {
var windowsVersion = stdout.match(/([0-9.])+/g)[0];
if (parseInt(windowsVersion) < 10) {
reject(new Error('The browser target is not supported on this version of Windows: %target%'));
} else {
resolve();
}
}
});
});
return prom;
}
var regItemPattern = /\s*\(Default\)\s+(REG_SZ)\s+([^\s].*)\s*/;
function browserInstalled (browser) {
// On Windows, the 'start' command searches the path then 'App Paths' in the registry.
// We do the same here. Note that the start command uses the PATHEXT environment variable
// for the list of extensions to use if no extension is provided. We simplify that to just '.EXE'
// since that is what all the supported browsers use. Check path (simple but usually won't get a hit)
var promise = new Promise(function (resolve, reject) {
if (require('shelljs').which(browser)) {
return resolve();
} else {
var regQPre = 'reg QUERY "HKLM\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths\\';
var regQPost = '.EXE" /v ""';
var regQuery = regQPre + browser.split(' ')[0] + regQPost;
child_process.exec(regQuery, function (err, stdout, stderr) {
if (err) {
// The registry key does not exist, which just means the app is not installed.
reject(err);
} else {
var result = regItemPattern.exec(stdout);
if (fs.existsSync(trimRegPath(result[2]))) {
resolve();
} else {
// The default value is not a file that exists, which means the app is not installed.
reject(new Error(NOT_INSTALLED));
}
}
});
}
});
return promise;
}
function trimRegPath (path) {
// Trim quotes and whitespace
return path.replace(/^[\s"]+|[\s"]+$/g, '');
}