-
Notifications
You must be signed in to change notification settings - Fork 0
/
browser_windows.go
80 lines (63 loc) · 1.45 KB
/
browser_windows.go
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
package marionette
import (
"fmt"
"os/exec"
"strings"
"golang.org/x/sys/windows/registry"
)
var idMap map[string]BrowserType = map[string]BrowserType{
"Chrome": CHROME,
"Firefox": FIREFOX,
"Edge": EDGE,
}
func getDefaultBrowserProgID() (string, error) {
browserKey, err := registry.OpenKey(
registry.CURRENT_USER,
`SOFTWARE\Microsoft\Windows\Shell\Associations\UrlAssociations\http\UserChoice`,
registry.QUERY_VALUE,
)
if err != nil {
return "", err
}
progID, _, err := browserKey.GetStringValue("ProgId")
return progID, err
}
func DefaultBrowser() (BrowserType, error) {
id, err := getDefaultBrowserProgID()
if err != nil {
return UNDEFINED, err
}
browser := filterBrowserIDMap(id, idMap)
if browser == UNDEFINED {
return UNDEFINED, &UnknownBrowserType{}
}
return browser, nil
}
func GetBrowserPath() (string, error) {
progID, err := getDefaultBrowserProgID()
if err != nil {
return "", err
}
pathKey, err := registry.OpenKey(
registry.LOCAL_MACHINE,
fmt.Sprintf(`SOFTWARE\Classes\%v\shell\open\command`, progID),
registry.QUERY_VALUE,
)
if err != nil {
return "", err
}
pathValue, _, err := pathKey.GetStringValue("")
if err != nil {
return "", err
}
path := strings.Split(pathValue, "\"")[1]
path = strings.ReplaceAll(path, "\"", "")
return path, err
}
func OpenBrowser(args ...string) error {
path, err := GetBrowserPath()
if err != nil {
return err
}
return exec.Command(path, args...).Run()
}