@@ -26,9 +26,12 @@ type SupportedPlatforms = 'darwin'|'linux'|'win32'|'wsl';
26
26
27
27
const instances = new Set < Launcher > ( ) ;
28
28
29
+ type ValidPrefTypes = string | boolean | number
30
+
29
31
export interface Options {
30
32
startingUrl ?: string ;
31
33
chromeFlags ?: Array < string > ;
34
+ prefs ?: Record < string , ValidPrefTypes > ;
32
35
port ?: number ;
33
36
handleSIGINT ?: boolean ;
34
37
chromePath ?: string ;
@@ -105,6 +108,7 @@ class Launcher {
105
108
private chromePath ?: string ;
106
109
private ignoreDefaultFlags ?: boolean ;
107
110
private chromeFlags : string [ ] ;
111
+ private prefs : Record < string , ValidPrefTypes >
108
112
private requestedPort ?: number ;
109
113
private connectionPollInterval : number ;
110
114
private maxConnectionRetries : number ;
@@ -127,6 +131,7 @@ class Launcher {
127
131
// choose the first one (default)
128
132
this . startingUrl = defaults ( this . opts . startingUrl , 'about:blank' ) ;
129
133
this . chromeFlags = defaults ( this . opts . chromeFlags , [ ] ) ;
134
+ this . prefs = defaults ( this . opts . prefs , { } ) ;
130
135
this . requestedPort = defaults ( this . opts . port , 0 ) ;
131
136
this . chromePath = this . opts . chromePath ;
132
137
this . ignoreDefaultFlags = defaults ( this . opts . ignoreDefaultFlags , false ) ;
@@ -197,6 +202,11 @@ class Launcher {
197
202
this . outFile = this . fs . openSync ( `${ this . userDataDir } /chrome-out.log` , 'a' ) ;
198
203
this . errFile = this . fs . openSync ( `${ this . userDataDir } /chrome-err.log` , 'a' ) ;
199
204
205
+ // set preferences
206
+ if ( Object . keys ( this . prefs ) . length ) {
207
+ this . setBrowserPrefs ( )
208
+ }
209
+
200
210
// fix for Node4
201
211
// you can't pass a fd to fs.writeFileSync
202
212
this . pidFile = `${ this . userDataDir } /chrome.pid` ;
@@ -206,6 +216,34 @@ class Launcher {
206
216
this . tmpDirandPidFileReady = true ;
207
217
}
208
218
219
+ private setBrowserPrefs ( ) {
220
+ const preferenceFile = `${ this . userDataDir } /Preferences`
221
+ try {
222
+ if ( this . fs . existsSync ( preferenceFile ) ) {
223
+ // overwrite existing file
224
+ const file = this . fs . readFileSync ( preferenceFile , 'utf-8' )
225
+ const content = JSON . parse ( file . toString ( ) )
226
+ this . fs . writeFileSync (
227
+ preferenceFile ,
228
+ JSON . stringify ( { ...content , ...this . prefs } ) ,
229
+ 'utf-8'
230
+ )
231
+ } else {
232
+ // create new Preference file
233
+ this . fs . writeFileSync (
234
+ preferenceFile ,
235
+ JSON . stringify ( { ...this . prefs } ) ,
236
+ 'utf-8'
237
+ )
238
+ }
239
+ } catch ( err ) {
240
+ log . log (
241
+ 'ChromeLauncher' ,
242
+ `Failed to set browser prefs: ${ err . message } `
243
+ )
244
+ }
245
+ }
246
+
209
247
async launch ( ) {
210
248
if ( this . requestedPort !== 0 ) {
211
249
this . port = this . requestedPort ;
@@ -259,7 +297,9 @@ class Launcher {
259
297
{ detached : true , stdio : [ 'ignore' , this . outFile , this . errFile ] , env : this . envVars } ) ;
260
298
this . chrome = chrome ;
261
299
262
- this . fs . writeFileSync ( this . pidFile , chrome . pid . toString ( ) ) ;
300
+ if ( chrome . pid ) {
301
+ this . fs . writeFileSync ( this . pidFile , chrome . pid . toString ( ) ) ;
302
+ }
263
303
264
304
log . verbose ( 'ChromeLauncher' , `Chrome running with pid ${ chrome . pid } on port ${ this . port } .` ) ;
265
305
return chrome . pid ;
@@ -347,7 +387,9 @@ class Launcher {
347
387
// if you don't explicitly set `stdio`
348
388
execSync ( `taskkill /pid ${ this . chrome . pid } /T /F` , { stdio : 'pipe' } ) ;
349
389
} else {
350
- process . kill ( - this . chrome . pid ) ;
390
+ if ( this . chrome . pid ) {
391
+ process . kill ( - this . chrome . pid ) ;
392
+ }
351
393
}
352
394
} catch ( err ) {
353
395
const message = `Chrome could not be killed ${ err . message } ` ;
0 commit comments