-
Notifications
You must be signed in to change notification settings - Fork 3
/
processUrl.go
248 lines (223 loc) · 6.22 KB
/
processUrl.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
//go:build windows
// +build windows
package main
import (
"fmt"
"net/http"
"net/url"
"strings"
"time"
"github.com/lxn/walk"
walkd "github.com/lxn/walk/declarative"
"github.com/stuartleeks/pick-a-browser/pkg/config"
)
func HandleUrl(urlString string, settings *config.Settings) error {
urlDisplayString := "<not specified>"
if urlString != "" {
url, err := url.Parse(urlString)
if err != nil {
return fmt.Errorf("failed to parse url %q:\n%s", urlString, err)
}
linkWrappers := append(config.GetDefaultLinkWrappers(), settings.Transformations.LinkWrappers...)
linkShorteners := append(config.GetDefaultLinkShorteners(), settings.Transformations.LinkShorteners...)
for {
// TODO - decide what to do with errors here
newUrl, _ := transformUrlWithWrappers(url, linkWrappers)
if newUrl != nil {
url = newUrl
continue
}
newUrl, _ = transformUrlWithShorteners(url, linkShorteners)
if newUrl != nil {
url = newUrl
continue
}
break
}
urlString = url.String()
urlDisplayString = urlString
// match rules - launch browser and exit on match, or fall through to show list
matchedBrowserId := matchRules(settings.Rules, url)
// if browser is set and browser != _prompt_, launch the browser
if matchedBrowserId != "_prompt_" && matchedBrowserId != "" {
// get browser from Id
for _, browser := range settings.Browsers {
if browser.Id == matchedBrowserId {
return browser.Launch(urlString)
}
}
return fmt.Errorf("failed to find browser with id %q", matchedBrowserId)
}
}
const maxDisplayLength = 75
if len(urlDisplayString) > maxDisplayLength {
urlDisplayString = urlDisplayString[:maxDisplayLength] + "..."
}
// If here then show browser list (filter to non-hidden browsers)
browsers := []config.Browser{}
for _, browser := range settings.Browsers {
if !browser.Hidden {
browsers = append(browsers, browser)
}
}
mw := &MyMainWindow{}
var copyButton *walk.PushButton
defaultFont := walkd.Font{Family: "Segoe UI", PointSize: 16}
widgets := []walkd.Widget{
walkd.Composite{
Layout: walkd.HBox{},
Row: 0,
Children: []walkd.Widget{
walkd.Label{
AssignTo: &mw.urlLabel,
Font: defaultFont,
Text: "URL: ",
},
walkd.Label{
AssignTo: &mw.urlLabel,
Font: defaultFont,
Text: urlDisplayString,
},
walkd.PushButton{
AssignTo: ©Button,
Text: "📋",
Font: walkd.Font{Family: "Segoe UI", PointSize: 20},
MaxSize: walkd.Size{Width: 30, Height: 15},
OnClicked: func() {
err := walk.Clipboard().SetText(urlString)
if err != nil {
walk.MsgBox(
mw.MainWindow,
"pick-a-browser error...",
fmt.Sprintf("Failed to copy: %s", err),
walk.MsgBoxOK|walk.MsgBoxIconError)
return
}
_ = copyButton.SetText("✅")
go func() {
time.Sleep(1 * time.Second)
_ = copyButton.SetText("📋")
}()
},
},
},
},
walkd.Label{
Font: defaultFont,
Text: fmt.Sprintf("Version: %s (%s, %s)", version, commit, date),
Row: len(browsers) + 1,
},
}
var innerErr error = nil
for _i, tmp := range browsers {
browserNumber := _i + 1
browser := tmp
widgets = append(widgets, walkd.PushButton{
Text: fmt.Sprintf("&%d: %s", browserNumber, browser.Name),
Row: browserNumber,
MinSize: walkd.Size{Width: 150, Height: 10},
MaxSize: walkd.Size{Width: 3000, Height: 150},
Font: walkd.Font{Family: "Segoe UI", PointSize: 20},
Alignment: walkd.AlignHCenterVNear,
OnClicked: func() {
innerErr = browser.Launch(urlString)
mw.Close()
},
})
}
window := walkd.MainWindow{
AssignTo: &mw.MainWindow,
Title: "pick-a-browser...",
MinSize: walkd.Size{Width: 150, Height: 150},
Size: walkd.Size{Width: 300, Height: 400},
MaxSize: walkd.Size{Width: 300, Height: 1000},
Layout: walkd.Grid{
MarginsZero: true,
Rows: len(settings.Browsers) + 2,
Margins: walkd.Margins{Top: 15, Bottom: 15, Left: 0, Right: 0},
Alignment: walkd.AlignHCenterVNear,
},
Children: widgets,
// OnKeyDown isn't being invoked :-(
// OnKeyDown: func(key walk.Key) {
// log.Println("keydown")
// walk.MsgBox(mw, "test", fmt.Sprintf("key: %v", key), walk.MsgBoxOK)
// browserIndex := -1
// switch key {
// case walk.Key1:
// browserIndex = 0
// case walk.Key2:
// browserIndex = 1
// case walk.Key3:
// browserIndex = 2
// case walk.Key4:
// browserIndex = 3
// case walk.Key5:
// browserIndex = 4
// case walk.Key6:
// browserIndex = 5
// case walk.Key7:
// browserIndex = 6
// case walk.Key8:
// browserIndex = 7
// case walk.Key9:
// browserIndex = 8
// default:
// return
// }
// browser := settings.Browsers[browserIndex]
// walk.MsgBox(mw, "test", fmt.Sprintf("TODO: launch %q", browser.Name), walk.MsgBoxOK)
// },
}
if _, err := window.Run(); err != nil {
return err
}
return innerErr
}
type MyMainWindow struct {
*walk.MainWindow
urlLabel *walk.Label
}
// TODO move out of main and add tests
func matchRules(rules []config.Rule, url *url.URL) string {
matchWeight := -1
browserId := ""
for _, rule := range rules {
tmpWeight := rule.Match(url)
if tmpWeight > matchWeight {
matchWeight = tmpWeight
browserId = rule.BrowserId()
}
}
return browserId
}
func transformUrlWithShorteners(url *url.URL, linkShorteners []string) (*url.URL, error) {
for _, linkShortener := range linkShorteners {
if strings.HasSuffix(url.Host, linkShortener) {
client := &http.Client{
CheckRedirect: func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
},
}
resp, err := client.Get(url.String())
if err != nil {
return nil, err
}
newUrlString := resp.Header.Get("Location")
return url.Parse(newUrlString)
}
}
return nil, nil
}
func transformUrlWithWrappers(url *url.URL, linkWrappers []config.LinkWrapper) (*url.URL, error) {
for _, linkWrapper := range linkWrappers {
if strings.HasPrefix(url.String(), linkWrapper.UrlPrefix) {
newUrlString := url.Query().Get(linkWrapper.QueryStringKey)
if newUrlString == "" {
return nil, nil
}
return url.Parse(newUrlString)
}
}
return nil, nil
}