@@ -66,6 +66,8 @@ func New() *Launcher {
6666 // enable headless by default
6767 flags .Headless : nil ,
6868
69+ flags .Preferences : {`{"plugins":{"always_open_pdf_externally": true}}` },
70+
6971 // to disable the init blank window
7072 "no-first-run" : nil ,
7173 "no-startup-window" : nil ,
@@ -242,6 +244,11 @@ func (l *Launcher) XVFB(args ...string) *Launcher {
242244 return l .Set (flags .XVFB , args ... )
243245}
244246
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+
245252// Leakless switch. If enabled, the browser will be force killed after the Go process exits.
246253// The doc of leakless: https://github.com/ysmood/leakless.
247254func (l * Launcher ) Leakless (enable bool ) * Launcher {
@@ -293,9 +300,9 @@ func (l *Launcher) UserDataDir(dir string) *Launcher {
293300// Related article: https://superuser.com/a/377195
294301func (l * Launcher ) ProfileDir (dir string ) * Launcher {
295302 if dir == "" {
296- l .Delete ("profile-directory" )
303+ l .Delete (flags . ProfileDir )
297304 } else {
298- l .Set ("profile-directory" , dir )
305+ l .Set (flags . ProfileDir , dir )
299306 }
300307 return l
301308}
@@ -394,19 +401,23 @@ func (l *Launcher) Launch() (string, error) {
394401 return "" , err
395402 }
396403
404+ l .setupUserPreferences ()
405+
397406 var ll * leakless.Launcher
398407 var cmd * exec.Cmd
399408
409+ args := l .FormatArgs ()
410+
400411 if l .Has (flags .Leakless ) && leakless .Support () {
401412 ll = leakless .New ()
402- cmd = ll .Command (bin , l . FormatArgs () ... )
413+ cmd = ll .Command (bin , args ... )
403414 } else {
404415 port := l .Get (flags .RemoteDebuggingPort )
405416 u , err := ResolveURL (port )
406417 if err == nil {
407418 return u , nil
408419 }
409- cmd = exec .Command (bin , l . FormatArgs () ... )
420+ cmd = exec .Command (bin , args ... )
410421 }
411422
412423 l .setupCmd (cmd )
@@ -443,6 +454,31 @@ func (l *Launcher) hasLaunched() bool {
443454 return ! atomic .CompareAndSwapInt32 (& l .isLaunched , 0 , 1 )
444455}
445456
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+
446482func (l * Launcher ) setupCmd (cmd * exec.Cmd ) {
447483 l .osSetupCmd (cmd )
448484
0 commit comments