@@ -13,11 +13,11 @@ import (
13
13
"github.com/go-rod/rod/lib/proto"
14
14
)
15
15
16
- // newBrwsrLauncher creates a new browser launcher with the given headless
17
- // mode.
16
+ // newBrwsrLauncher creates a new incognito browser launcher with the given
17
+ // headless mode.
18
18
func (c * Client ) newBrwsrLauncher (headless bool ) * launcher.Launcher {
19
19
l := launcher .New ().Headless (headless ).Leakless (isLeaklessEnabled ).Devtools (false )
20
- if binpath , ok := lookPath (); ! c .opts .useBundledBrwsr && ok {
20
+ if binpath , ok := c .opts .browserPath (); ok {
21
21
l = l .Bin (binpath )
22
22
}
23
23
return l
@@ -26,60 +26,116 @@ func (c *Client) newBrwsrLauncher(headless bool) *launcher.Launcher {
26
26
// usrBrwsrLauncher creates a new user-mode browser launcher.
27
27
func (c * Client ) usrBrwsrLauncher () * launcher.Launcher {
28
28
l := launcher .NewUserMode ().Headless (false ).Leakless (isLeaklessEnabled ).Devtools (false )
29
- if binpath , ok := lookPath (); ! c .opts .useBundledBrwsr && ok {
29
+ if binpath , ok := c .opts .browserPath (); ok {
30
30
l = l .Bin (binpath )
31
31
}
32
32
return l
33
33
}
34
34
35
+ // browserPath returns the path to the browser executable and a boolean
36
+ // indicating whether the path is valid.
37
+ func (o options ) browserPath () (path string , ok bool ) {
38
+ if o .useBundledBrwsr && ! o .forceUser {
39
+ // bundled browser can't operate in the user mode. forceUser overrides
40
+ // useBundledBrwsr.
41
+ return "" , false
42
+ }
43
+ if o .localBrowser != "" {
44
+ if p , err := exec .LookPath (o .localBrowser ); err == nil {
45
+ return p , true
46
+ }
47
+ }
48
+ return lookPath ()
49
+ }
50
+
51
+ var ErrNoBrowsers = fmt .Errorf ("no browsers found" )
52
+
53
+ // ListBrowsers returns a list of browsers that are installed on the system.
54
+ func ListBrowsers () ([]LocalBrowser , error ) {
55
+ LocalBrowsers , ok := discover ()
56
+ if ! ok {
57
+ return nil , ErrNoBrowsers
58
+ }
59
+ return LocalBrowsers , nil
60
+ }
61
+
62
+ const (
63
+ bChrome = "Google Chrome"
64
+ bChromium = "Chromium"
65
+ bEdge = "Microsoft Edge"
66
+ bBrave = "Brave"
67
+ )
68
+
69
+ // LocalBrowser represents a browser that is installed on the system.
70
+ type LocalBrowser struct {
71
+ Name string
72
+ Path string
73
+ }
74
+
75
+ // discover returns the list of browsers that are installed on the system and a
76
+ // boolean indicating whether any browsers were found.
77
+ func discover () (found []LocalBrowser , has bool ) {
78
+ for _ , br := range browserList {
79
+ var err error
80
+ p , err := exec .LookPath (br .Path )
81
+ if err == nil {
82
+ found = append (found , LocalBrowser {br .Name , p })
83
+ }
84
+ }
85
+
86
+ return found , len (found ) > 0
87
+ }
88
+
89
+ var browserList = map [string ][]LocalBrowser {
90
+ "darwin" : {
91
+ {bBrave , "/Applications/Brave Browser.app/Contents/MacOS/Brave Browser" },
92
+ {bEdge , "/Applications/Microsoft Edge.app/Contents/MacOS/Microsoft Edge" },
93
+ {bChrome , "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome" },
94
+ {bChromium , "/Applications/Chromium.app/Contents/MacOS/Chromium" },
95
+ {bChrome , "/Applications/Google Chrome Canary.app/Contents/MacOS/Google Chrome Canary" },
96
+ {bChrome , "/usr/bin/google-chrome-stable" },
97
+ {bChrome , "/usr/bin/google-chrome" },
98
+ {bChromium , "/usr/bin/chromium" },
99
+ {bChromium , "/usr/bin/chromium-browser" },
100
+ },
101
+ "linux" : {
102
+ {bChrome , "brave-browser" },
103
+ {bChrome , "chrome" },
104
+ {bChrome , "google-chrome" },
105
+ {bChrome , "/usr/bin/google-chrome" },
106
+ {bChrome , "/usr/bin/brave-browser" },
107
+ {bChrome , "microsoft-edge" },
108
+ {bChrome , "/usr/bin/microsoft-edge" },
109
+ {bChrome , "chromium" },
110
+ {bChrome , "chromium-browser" },
111
+ {bChrome , "/usr/bin/google-chrome-stable" },
112
+ {bChrome , "/usr/bin/chromium" },
113
+ {bChrome , "/usr/bin/chromium-browser" },
114
+ {bChrome , "/snap/bin/chromium" },
115
+ {bChrome , "/data/data/com.termux/files/usr/bin/chromium-browser" },
116
+ },
117
+ "openbsd" : {
118
+ {bChrome , "chrome" },
119
+ {bChrome , "chromium" },
120
+ },
121
+ "windows" : append (
122
+ []LocalBrowser {{bEdge , "edge" }, {bBrave , "brave" }, {bChrome , "chrome" }},
123
+ expandWindowsExePaths (
124
+ LocalBrowser {bEdge , `Microsoft\Edge\Application\msedge.exe` },
125
+ LocalBrowser {bEdge , `BraveSoftware\Brave-Browser\Application\brave.exe` },
126
+ LocalBrowser {bEdge , `Google\Chrome\Application\chrome.exe` },
127
+ LocalBrowser {bEdge , `Chromium\Application\chrome.exe` },
128
+ )... ),
129
+ }[runtime .GOOS ]
130
+
35
131
// lookPath is extended launcher.LookPath that includes support for Brave
36
132
// browser.
37
133
//
38
134
// (c) MIT license: Copyright 2019 Yad Smood
39
135
func lookPath () (found string , has bool ) {
40
- list := map [string ][]string {
41
- "darwin" : {
42
- "/Applications/Brave Browser.app/Contents/MacOS/Brave Browser" ,
43
- "/Applications/Microsoft Edge.app/Contents/MacOS/Microsoft Edge" ,
44
- "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome" ,
45
- "/Applications/Chromium.app/Contents/MacOS/Chromium" ,
46
- "/Applications/Google Chrome Canary.app/Contents/MacOS/Google Chrome Canary" ,
47
- "/usr/bin/google-chrome-stable" ,
48
- "/usr/bin/google-chrome" ,
49
- "/usr/bin/chromium" ,
50
- "/usr/bin/chromium-browser" ,
51
- },
52
- "linux" : {
53
- "brave-browser" ,
54
- "chrome" ,
55
- "google-chrome" ,
56
- "/usr/bin/google-chrome" ,
57
- "/usr/bin/brave-browser" ,
58
- "microsoft-edge" ,
59
- "/usr/bin/microsoft-edge" ,
60
- "chromium" ,
61
- "chromium-browser" ,
62
- "/usr/bin/google-chrome-stable" ,
63
- "/usr/bin/chromium" ,
64
- "/usr/bin/chromium-browser" ,
65
- "/snap/bin/chromium" ,
66
- "/data/data/com.termux/files/usr/bin/chromium-browser" ,
67
- },
68
- "openbsd" : {
69
- "chrome" ,
70
- "chromium" ,
71
- },
72
- "windows" : append ([]string {"chrome" , "edge" }, expandWindowsExePaths (
73
- `Microsoft\Edge\Application\msedge.exe` ,
74
- `BraveSoftware\Brave-Browser\Application\brave.exe` ,
75
- `Google\Chrome\Application\chrome.exe` ,
76
- `Chromium\Application\chrome.exe` ,
77
- )... ),
78
- }[runtime .GOOS ]
79
-
80
- for _ , path := range list {
136
+ for _ , b := range browserList {
81
137
var err error
82
- found , err = exec .LookPath (path )
138
+ found , err = exec .LookPath (b . Path )
83
139
has = err == nil
84
140
if has {
85
141
break
@@ -89,18 +145,17 @@ func lookPath() (found string, has bool) {
89
145
return
90
146
}
91
147
92
- // expandWindowsExePaths is a verbatim copy of the function from rod's
93
- // browser.go.
148
+ // expandWindowsExePaths is based on the same function from rod's browser.go.
94
149
//
95
150
// (c) MIT license: Copyright 2019 Yad Smood
96
- func expandWindowsExePaths (list ... string ) []string {
97
- newList := []string {}
151
+ func expandWindowsExePaths (list ... LocalBrowser ) []LocalBrowser {
152
+ newList := []LocalBrowser {}
98
153
for _ , p := range list {
99
154
newList = append (
100
155
newList ,
101
- filepath .Join (os .Getenv ("ProgramFiles" ), p ) ,
102
- filepath .Join (os .Getenv ("ProgramFiles(x86)" ), p ) ,
103
- filepath .Join (os .Getenv ("LocalAppData" ), p ) ,
156
+ LocalBrowser { p . Name , filepath .Join (os .Getenv ("ProgramFiles" ), p . Path )} ,
157
+ LocalBrowser { p . Name , filepath .Join (os .Getenv ("ProgramFiles(x86)" ), p . Path )} ,
158
+ LocalBrowser { p . Name , filepath .Join (os .Getenv ("LocalAppData" ), p . Path )} ,
104
159
)
105
160
}
106
161
@@ -120,3 +175,18 @@ func setCookies(browser *rod.Browser, cookies []*http.Cookie) error {
120
175
}
121
176
return nil
122
177
}
178
+
179
+ // RemveBundled removes the bundled browser from the system.
180
+ func RemoveBrowser () error {
181
+ bpath := launcher .DefaultBrowserDir
182
+ if bpath == "" {
183
+ return nil
184
+ }
185
+ if _ , err := os .Stat (bpath ); os .IsNotExist (err ) {
186
+ return nil
187
+ }
188
+ if err := os .RemoveAll (bpath ); err != nil {
189
+ return fmt .Errorf ("failed to remove bundled browser: %w" , err )
190
+ }
191
+ return nil
192
+ }
0 commit comments