@@ -66,6 +66,8 @@ func New() *Launcher {
66
66
// enable headless by default
67
67
flags .Headless : nil ,
68
68
69
+ flags .Preferences : {`{"plugins":{"always_open_pdf_externally": true}}` },
70
+
69
71
// to disable the init blank window
70
72
"no-first-run" : nil ,
71
73
"no-startup-window" : nil ,
@@ -242,6 +244,11 @@ func (l *Launcher) XVFB(args ...string) *Launcher {
242
244
return l .Set (flags .XVFB , args ... )
243
245
}
244
246
247
+ // Preferences set chromium user preferences, such as set the default search engine or disable the pdf viewer.
248
+ func (l * Launcher ) Preferences (pref string ) * Launcher {
249
+ return l .Set (flags .Preferences , pref )
250
+ }
251
+
245
252
// Leakless switch. If enabled, the browser will be force killed after the Go process exits.
246
253
// The doc of leakless: https://github.com/ysmood/leakless.
247
254
func (l * Launcher ) Leakless (enable bool ) * Launcher {
@@ -293,9 +300,9 @@ func (l *Launcher) UserDataDir(dir string) *Launcher {
293
300
// Related article: https://superuser.com/a/377195
294
301
func (l * Launcher ) ProfileDir (dir string ) * Launcher {
295
302
if dir == "" {
296
- l .Delete ("profile-directory" )
303
+ l .Delete (flags . ProfileDir )
297
304
} else {
298
- l .Set ("profile-directory" , dir )
305
+ l .Set (flags . ProfileDir , dir )
299
306
}
300
307
return l
301
308
}
@@ -394,19 +401,23 @@ func (l *Launcher) Launch() (string, error) {
394
401
return "" , err
395
402
}
396
403
404
+ l .setupUserPreferences ()
405
+
397
406
var ll * leakless.Launcher
398
407
var cmd * exec.Cmd
399
408
409
+ args := l .FormatArgs ()
410
+
400
411
if l .Has (flags .Leakless ) && leakless .Support () {
401
412
ll = leakless .New ()
402
- cmd = ll .Command (bin , l . FormatArgs () ... )
413
+ cmd = ll .Command (bin , args ... )
403
414
} else {
404
415
port := l .Get (flags .RemoteDebuggingPort )
405
416
u , err := ResolveURL (port )
406
417
if err == nil {
407
418
return u , nil
408
419
}
409
- cmd = exec .Command (bin , l . FormatArgs () ... )
420
+ cmd = exec .Command (bin , args ... )
410
421
}
411
422
412
423
l .setupCmd (cmd )
@@ -443,6 +454,31 @@ func (l *Launcher) hasLaunched() bool {
443
454
return ! atomic .CompareAndSwapInt32 (& l .isLaunched , 0 , 1 )
444
455
}
445
456
457
+ func (l * Launcher ) setupUserPreferences () {
458
+ userDir := l .Get (flags .UserDataDir )
459
+ if userDir == "" {
460
+ return
461
+ }
462
+
463
+ userDir , err := filepath .Abs (userDir )
464
+ utils .E (err )
465
+
466
+ profile := l .Get (flags .ProfileDir )
467
+ if profile == "" {
468
+ profile = "Default"
469
+ }
470
+
471
+ path := filepath .Join (userDir , profile , "Preferences" )
472
+
473
+ pref := l .Get (flags .Preferences )
474
+
475
+ if pref == "" {
476
+ pref = "{}"
477
+ }
478
+
479
+ utils .E (utils .OutputFile (path , pref ))
480
+ }
481
+
446
482
func (l * Launcher ) setupCmd (cmd * exec.Cmd ) {
447
483
l .osSetupCmd (cmd )
448
484
0 commit comments