-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchromer.go
More file actions
204 lines (170 loc) · 4.42 KB
/
chromer.go
File metadata and controls
204 lines (170 loc) · 4.42 KB
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package main
// Heavily adopted and modified from: https://gist.github.com/nathankerr/38d8b0d45590741b57f5f79be336f07c/revisions
// Get Chrome profile names from: chrome://version/
/*
#cgo CFLAGS: -Ibuild -x objective-c
#cgo LDFLAGS: -framework Foundation
#include "handler.h"
*/
import "C"
import (
"bufio"
"fmt"
"log"
"os"
"regexp"
"strings"
"sync"
"syscall"
"github.com/fsnotify/fsnotify"
"github.com/andlabs/ui"
)
var labelText chan string
var updateConfig chan bool
type configBlock struct {
profile string
regex *regexp.Regexp
}
func main() {
cfg := os.Getenv("HOME") + "/.chromer"
logfh, err := os.Create(os.Getenv("HOME") + "/.chromer.log")
if err != nil {
log.Fatal(err)
}
defer logfh.Close()
logger := log.New(logfh, "chromer: ", log.LstdFlags)
// Load the mandatory config data
configs, err := loadConfig(cfg)
if err != nil {
log.Fatal(err)
}
// Prepare to receive the clicked URL
labelText = make(chan string, 1)
updateConfig = make(chan bool, 1)
monitorConfig(cfg, updateConfig, logger)
C.StartURLHandler()
wg := sync.WaitGroup{}
wg.Add(1)
if err := ui.Main(func() {
go func() {
defer wg.Done()
for {
select {
case url := <-labelText:
ui.QueueMain(func() {
launchURL(configs, url, logger)
})
case <-updateConfig:
configs, _ = loadConfig(cfg)
}
}
}()
}); err != nil {
log.Fatal(err)
}
wg.Wait()
}
//export HandleURL
func HandleURL(u *C.char) {
labelText <- C.GoString(u)
}
func monitorConfig(cfg string, ch chan bool, logger *log.Logger) error {
watcher, err := fsnotify.NewWatcher()
if err != nil {
return err
}
go func() {
for {
select {
case event, ok := <-watcher.Events:
if !ok {
logger.Fatal("bailing out due to fsnotify event listening error")
}
logger.Println("event:", event)
if event.Op&(fsnotify.Write|fsnotify.Rename) != 0 {
logger.Println("modified file:", event.Name)
ch <- true
}
case err, ok := <-watcher.Errors:
if !ok {
logger.Fatal("bailing out due to fsnotify error listening error")
}
logger.Println("error:", err)
}
}
}()
return watcher.Add(cfg)
}
func loadConfig(cfg string) ([]configBlock, error) {
var err error
var fh *os.File
if fh, err = os.Open(cfg); err != nil {
return nil, err
}
defer fh.Close()
var profile string
var patterns []string
var config []configBlock
scanner := bufio.NewScanner(fh)
for scanner.Scan() {
// Sanitized line from config
line := strings.TrimSpace(scanner.Text())
// Extract the profile name block
if strings.HasPrefix(line, "[") && strings.HasSuffix(line, "]") {
if len(profile) > 0 {
if len(patterns) > 0 {
config = append(config, configBlock{profile,
regexp.MustCompile(fmt.Sprintf("(?i)\\b(%s)\\b", strings.Join(patterns, "|")))})
patterns = nil
} else {
config = append(config, configBlock{profile, nil})
}
}
profile = strings.Trim(line, "[]")
} else if len(line) > 0 && !strings.HasPrefix(line, "#") {
patterns = append(patterns, line)
}
}
// Catch the last config block
if len(profile) > 0 && len(patterns) > 0 {
config = append(config, configBlock{profile,
regexp.MustCompile(fmt.Sprintf("(?i)\\b(%s)\\b", strings.Join(patterns, "|")))})
patterns = nil
}
return config, nil
}
func launchURL(configs []configBlock, url string, logger *log.Logger) error {
browser, profile := getProfile(configs, url)
var args []string
if browser == "firefox" {
args = []string{"/Applications/Firefox.app/Contents/MacOS/firefox", "-P", profile, "-new-tab", "-url", url}
} else if browser == "chrome" {
args = []string{"/Applications/Google Chrome.app/Contents/MacOS/Google Chrome",
fmt.Sprintf("--profile-directory=%s", profile), "-t", url}
} else if browser == "brave" {
args = []string{"/usr/bin/open", "-a", "Brave Browser", url, "--args", "-t"}
} else {
args = []string{"/usr/bin/open", "-a", browser, url}
}
if _, err := syscall.ForkExec(args[0], args, nil); err != nil {
return err
}
return nil
}
func getProfile(configs []configBlock, url string) (string, string) {
urlBytes := []byte(url)
var profile string
for _, cfg := range configs {
if len(profile) == 0 && cfg.regex == nil {
profile = cfg.profile
} else if cfg.regex != nil && cfg.regex.Match(urlBytes) {
profile = cfg.profile
break
}
}
fields := strings.Split(profile, "|")
if len(fields) == 1 {
return "chrome", fields[0]
}
return strings.ToLower(fields[0]), fields[1]
}