-
Notifications
You must be signed in to change notification settings - Fork 0
/
headless_browser.go
148 lines (127 loc) · 3.67 KB
/
headless_browser.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
package main
import (
"context"
"errors"
"fmt"
"log"
"net/url"
"strings"
"time"
"github.com/chromedp/cdproto/network"
"github.com/chromedp/chromedp"
)
type HeadlessBrowser struct {
Info *InstanceInfo
stopChan chan bool
UseHeuristics bool
Quiet bool
}
// NewHeadlessBrowser connects to a headless browser at remote.
// If quiet is true, debug output is suppressed.
func NewHeadlessBrowser(remote string, useHeuristics bool, quiet bool) (*HeadlessBrowser, error) {
ii, err := GetInstanceInfo(remote)
if err != nil {
return nil, fmt.Errorf("Unable to connect to instance: %q", err)
}
log.Printf("Found instace %q with User-Agent %q. Using debuggerURL %q.",
ii.Browser,
ii.UserAgent,
ii.WebSocketDebuggerURL,
)
return &HeadlessBrowser{
Info: ii,
stopChan: make(chan bool, 1),
UseHeuristics: useHeuristics,
Quiet: quiet,
}, nil
}
// ExtractURL visits the given targetURL until it finds a new url that is accepted by matcherFunc or timeout expires.
func (hb *HeadlessBrowser) ExtractURL(targetURL string, timeout time.Duration, resultChan chan *network.Request, matcherFunc func(url *url.URL) bool) error {
log.Printf("extracting from %q", targetURL)
timeoutTicker := time.NewTicker(timeout)
// source: https://github.com/chromedp/chromedp/blob/master/allocate_test.go
allocCtx, allocCancel := chromedp.NewRemoteAllocator(context.Background(), hb.Info.WebSocketDebuggerURL)
defer allocCancel()
ctx, cancel := chromedp.NewContext(allocCtx)
defer cancel()
chromedp.ListenTarget(ctx, func(ev interface{}) {
switch ev := ev.(type) {
case *network.EventWebSocketCreated:
if hb.Quiet {
break
}
log.Printf("WEBSOCKET: %q", ev.URL)
case *network.EventLoadingFailed:
if hb.Quiet {
break
}
log.Printf("FAILED: %q", ev.ErrorText)
case *network.EventRequestWillBeSent:
if !hb.Quiet {
log.Printf("REQUEST: %q", ev.Request.URL)
}
url, err := url.Parse(ev.Request.URL)
if err != nil {
log.Printf("request %q error: %q", ev.Request.URL, err)
}
if ev.Request.URL != targetURL && matcherFunc(url) {
// Navigation stalls if channel is blocked...
go func() { resultChan <- ev.Request }()
}
}
})
if err := chromedp.Run(ctx,
network.Enable(), // enable network events
chromedp.Navigate(targetURL), // navigate to url
); err != nil {
return err
}
log.Println("waiting for page to finish loading...")
err := waitToFinishLoading(ctx, timeoutTicker)
if err != nil {
return err
}
if hb.UseHeuristics {
clickAll(ctx)
}
log.Printf("waiting to find matching urls...")
select {
case <-timeoutTicker.C:
chromedp.Run(ctx,
chromedp.Stop(),
)
return errors.New("timeout")
case <-hb.stopChan:
chromedp.Run(ctx,
chromedp.Stop(),
)
log.Println("stopped!")
return nil
}
}
// Stop trys to abort ExtractURL and shuts down the headless browser instance.
func (hb *HeadlessBrowser) Stop() {
hb.stopChan <- true
}
// waitToFinishLoading waits for site to finish loading (since clicking buttons mights not work correctly otherwise)
// source: https://github.com/chromedp/chromedp/issues/252
// Only returns with an error on timeout.
func waitToFinishLoading(ctx context.Context, timeoutTicker *time.Ticker) error {
state := "notloaded"
script := `document.readyState`
checkTicker := time.NewTicker(time.Millisecond * 100)
for {
select {
case <-checkTicker.C:
err := chromedp.Run(ctx, chromedp.EvaluateAsDevTools(script, &state))
if err != nil {
log.Printf("error in eval: %q", err)
}
if strings.Compare(state, "complete") == 0 {
return nil
}
case <-timeoutTicker.C:
return errors.New("timeout while waiting to finish loading")
}
}
}