-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
184 lines (162 loc) · 4.16 KB
/
main.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package kiosk
import (
"io/ioutil"
"os"
"path"
"reflect"
"runtime"
"sort"
"strings"
homedir "github.com/mitchellh/go-homedir"
)
//Browser has a printable name, a way to execute it and a "rank" (if chrome and ie are both installed, choose chrome instead of ie because of better support for kiosk mode and other features). Lower rank is better
type Browser struct {
Name string
Rank int
Exec func(url string) error
}
//BrowserOptions should be passed to .Exec in Browser
// type BrowserOptions struct {
// Dimensions []int
// FullScreen bool
// }
type browserSlice []Browser
func (b browserSlice) Len() int {
return len(b)
}
func (b browserSlice) Swap(i, j int) {
b[i], b[j] = b[j], b[i]
}
func (b browserSlice) Less(i, j int) bool {
return b[i].Rank < b[j].Rank
}
//Browsers are held in a map, accessible with their 'shorthand' representations
var browsers = map[string]Browser{
"firefox": Browser{
"Firefox",
1,
func(url string) error {
//get root dir path
root, err := homedir.Expand("~/.mozilla/firefox/")
if err != nil {
return err
}
//read that dir
files, er := ioutil.ReadDir(root)
if er != nil {
return er
}
//try to find a .noui folder
var dir string = ""
for _, f := range files {
if strings.Index(f.Name(), ".noui") > -1 {
dir = f.Name()
}
}
//make that profile!
if dir == "" {
out, er := spawnCmd("firefox", "-CreateProfile", "noui", "-no-remote")
if er != nil {
return er
}
dir = strings.Replace(out, "Success: created profile 'noui' at '", "", 0)
dir = dir[0 : len(dir)-1] //cut off last '
}
root = path.Join(root, dir, "chrome")
os.MkdirAll(root, os.ModePerm)
file := path.Join(root, "userChrome.css")
var f *os.File = nil
//check if userChrome already exists
if _, err := os.Stat(file); !os.IsNotExist(err) {
str, _ := ioutil.ReadFile(file)
//if this css code is not already in userChrome.css
if strings.Index((string)(str), firefoxstring) == -1 {
//append to userChrome.css
f, err = os.OpenFile(file, os.O_APPEND|os.O_WRONLY, 0600)
if err != nil {
return err
}
}
// fmt.Println("INFO: userChrome.css already exists. Appending to file now.")
} else {
f, err = os.Create(file)
if err != nil {
return err
}
}
if f != nil {
defer f.Close()
if _, err = f.WriteString(firefoxstring); err != nil {
return err
}
}
return runCmd("firefox", "-new-instance", "-P", "noui", url)
}},
"ie": Browser{
"Internet Explorer",
2,
func(url string) error {
return runCmd("iexplore", "-k", url)
}},
"chrome": Browser{
"Google Chrome",
0,
func(url string) error {
return runCmd("chrome", strings.Join([]string{"--app", url}, "="))
}},
"chromium": Browser{
"Chromium",
0,
func(url string) error {
return runCmd("chromium", strings.Join([]string{"--app", url}, "="))
}},
"safari": Browser{
"Safari",
2,
func(url string) error {
runCmd("safari", url)
return runCmd("osascript", osascript)
}}}
//BestBrowser returns the browser with the highest ranking
func BestBrowser(list []Browser) Browser {
lst := (browserSlice)(list)
sort.Sort(lst)
return lst[0]
}
//GetInstalled returns slice of Browsers that work on the user's machine, or nil if unknown OS is running
func GetInstalled() []Browser {
switch runtime.GOOS {
case "freebsd", "linux", "darwin":
return envLookup(":")
case "windows":
return envLookup(";")
}
return nil
}
func envLookup(splitter string) []Browser {
//searches the directories in $PATH
path := strings.Split(os.Getenv("PATH"), ":")
browserKeys := reflect.ValueOf(browsers).MapKeys()
valid := browserSlice{}
alreadyLogged := map[string]bool{}
//iterate dirs in $PATH
for _, d := range path {
files, _ := ioutil.ReadDir(d)
//iterate files
for _, f := range files {
//iterate Browsers
for _, b := range browserKeys {
//if file == Browser
if f.Name() == b.String() {
//if not already logged
if _, ok := alreadyLogged[f.Name()]; !ok {
alreadyLogged[f.Name()] = true
valid = append(valid, browsers[b.String()])
}
}
}
}
}
sort.Sort(valid) //sort according to preference
return valid
}