-
Notifications
You must be signed in to change notification settings - Fork 0
/
reloader.go
190 lines (165 loc) · 5.15 KB
/
reloader.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
package reloader
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os/exec"
"strings"
"golang.org/x/net/websocket"
)
// RemoteConfig is the configuration used to start chrome remote debugging
type RemoteConfig struct {
// ExecName is the name of the chrome executable (e.g "google-chrome", "chromium")
ExecName string
// DebugAddr is the chrome remote debugging address (default localhost)
DebugAddr string
// DebugPort is the chrome remote debugging port (default 9222)
DebugPort int
// UserDataDir is the folder for chrome user profile
UserDataDir string
// Addr is the base address to be opened
Addr string
// Port is the port for the base address to be opened
Port int
// Route path to be opened
Route string
}
// json returned from chrome remote debugging api
type chromeTab struct {
ID string `json:"id"`
Title string `json:"title"`
Type string `json:"type"`
URL string `json:"url"`
WebSocketDebuggerURL string `json:"webSocketDebuggerUrl"`
Description string `json:"description"`
}
// sent to chrome debugging port to reload tabs
type refreshJSON struct {
ID uint16 `json:"id"`
Method string `json:"method"`
Params parameters `json:"params"`
}
type parameters struct {
IgnoreCache bool `json:"ignoreCache"`
}
// NewRemoteConfig returns a default config for RemoteChrome
func NewRemoteConfig() *RemoteConfig {
return &RemoteConfig{
ExecName: "google-chrome",
DebugAddr: "localhost",
DebugPort: 9222,
UserDataDir: "/tmp/.chrome-remote-profile",
Addr: "localhost",
Port: 8080,
Route: "",
}
}
// RemoteChrome starts a new chrome remote debugging session
func (rc *RemoteConfig) RemoteChrome() (context.CancelFunc, error) {
ctx, cancel := context.WithCancel(context.Background())
cmd := exec.CommandContext(
ctx,
rc.ExecName,
fmt.Sprintf("--remote-debugging-port=%d", rc.DebugPort),
fmt.Sprintf("--user-data-dir=%s", rc.UserDataDir),
fmt.Sprintf("http://%s:%d%s", rc.Addr, rc.Port, rc.Route),
)
return cancel, cmd.Start()
}
// ReloadAllTabs will reload all opened tabs
func (rc *RemoteConfig) ReloadAllTabs() error {
return reloadTabs(rc.DebugAddr, rc.DebugPort, nil, "")
}
// ReloadTab reloads one chrome tab by checking if the tab URL has route as suffix
func (rc *RemoteConfig) ReloadTab(route string) error {
return reloadTabs(rc.DebugAddr, rc.DebugPort, strings.HasSuffix, route)
}
// ReloadTabGroup reloads a group of chrome tabs under the partial route: /partial/route/tabs/to/reload
func (rc *RemoteConfig) ReloadTabGroup(subroute string) error {
return reloadTabs(rc.DebugAddr, rc.DebugPort, strings.Contains, subroute)
}
// CloseAllTabs closes all opened tabs in browser (should close the browser)
func (rc *RemoteConfig) CloseAllTabs() error {
return closeTabs(rc.DebugAddr, rc.DebugPort, nil, "")
}
// CloseTab closes a given tab
func (rc *RemoteConfig) CloseTab(route string) error {
return closeTabs(rc.DebugAddr, rc.DebugPort, strings.HasSuffix, route)
}
// CloseTabGroup closes a group of chrome tabs under the partial route: /partial/route/tabs/to/close
func (rc *RemoteConfig) CloseTabGroup(route string) error {
return closeTabs(rc.DebugAddr, rc.DebugPort, strings.Contains, route)
}
func reloadTabs(addr string, port int, conditionFunc func(string, string) bool, route string) error {
tabs, err := getTabs(addr, port)
if err != nil {
return err
}
var e error
for _, tab := range tabs {
if conditionFunc == nil || conditionFunc(tab.URL, route) {
err := reloadTab(tab)
if err != nil {
e = err
}
}
}
return e
}
func reloadTab(tab chromeTab) error {
url := tab.WebSocketDebuggerURL
origin := tab.URL
ws, err := websocket.Dial(url, "", origin)
if err != nil {
return fmt.Errorf("error while connecting to socket: %s", err)
}
jsonStruct := &refreshJSON{ID: 0, Method: "Page.reload", Params: parameters{IgnoreCache: true}}
jsonString, err := json.Marshal(jsonStruct)
if err != nil {
return fmt.Errorf("error while marshalling json: %s", err)
}
_, err = ws.Write(jsonString)
if err != nil {
return fmt.Errorf("error while preparing json: %s", err)
}
return nil
}
func closeTabs(addr string, port int, conditionFunc func(string, string) bool, route string) error {
tabs, err := getTabs(addr, port)
if err != nil {
return err
}
var e error
for _, tab := range tabs {
if conditionFunc == nil || conditionFunc(tab.URL, route) {
err := closeTab(addr, port, tab)
if err != nil {
e = err
}
}
}
return e
}
func closeTab(addr string, port int, tab chromeTab) error {
_, err := http.Get(fmt.Sprintf("http://%s:%d/json/close/%s", addr, port, tab.ID))
if err != nil {
return fmt.Errorf("error while close tab: %s", err)
}
return nil
}
func getTabs(addr string, port int) ([]chromeTab, error) {
resp, err := http.Get(fmt.Sprintf("http://%s:%d/json", addr, port))
if err != nil {
return nil, fmt.Errorf("error while getting tabs: %s", err)
}
defer resp.Body.Close()
var tabs []chromeTab
buffer, _ := ioutil.ReadAll(resp.Body)
err = json.Unmarshal(buffer, &tabs)
if err != nil {
return nil, fmt.Errorf("error while unmarshalling tabs: %s", err)
}
return tabs, nil
}