-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevent.go
156 lines (137 loc) · 3.81 KB
/
event.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
package chrome
import (
"context"
"crypto/rand"
"encoding/hex"
"encoding/json"
"fmt"
"log/slog"
"net/http"
"regexp"
"strings"
"sync"
"github.com/chromedp/cdproto/network"
"github.com/chromedp/chromedp"
)
var DefaultChannelBufferCapacity = 8
type Event struct {
Request *network.EventRequestWillBeSent
Response *network.EventResponseReceived
Bytes []byte
}
func (e *Event) Header() http.Header {
header := make(http.Header)
if resp := e.Response; resp != nil {
for k, v := range resp.Response.Headers {
header.Set(k, fmt.Sprint(v))
}
}
return header
}
func (e *Event) Unmarshal(v any) error {
return json.Unmarshal(e.Bytes, v)
}
func (e *Event) String() string {
return string(e.Bytes)
}
func ListenEvent(ctx context.Context, url any, method string, download bool) <-chan *Event {
c, ec := make(chan *Event, DefaultChannelBufferCapacity), make(chan *Event, DefaultChannelBufferCapacity)
go func() {
<-ctx.Done()
close(ec)
close(c)
}()
var m sync.Map
chromedp.ListenTarget(ctx, func(v any) {
switch ev := v.(type) {
case *network.EventRequestWillBeSent:
if compare(ev.Request.URL, url) && (method == "" || strings.EqualFold(method, ev.Request.Method)) {
m.Store(ev.RequestID, &Event{ev, nil, nil})
}
case *network.EventResponseReceived:
if v, ok := m.Load(ev.RequestID); ok {
v.(*Event).Response = ev
if v.(*Event).Request.Request.Method == "HEAD" {
m.Delete(ev.RequestID)
go func() {
defer func() { recover() }()
ec <- v.(*Event)
}()
}
}
case *network.EventLoadingFinished:
if v, ok := m.LoadAndDelete(ev.RequestID); ok {
go func() {
defer func() { recover() }()
ec <- v.(*Event)
}()
}
}
})
go func() {
defer func() { recover() }()
for e := range ec {
if download {
if err := chromedp.Run(
ctx,
chromedp.ActionFunc(func(ctx context.Context) (err error) {
e.Bytes, err = network.GetResponseBody(e.Response.RequestID).Do(ctx)
return
}),
); err != nil {
slog.Debug(err.Error())
}
}
c <- e
}
}()
return c
}
func ListenScriptEvent(
ctx context.Context, script string, url any, method, variable string, download bool) (string, <-chan *Event, error) {
expression := fmt.Sprintf(script, "v")
if strings.HasSuffix(expression, "%!(EXTRA string=v)") {
expression = script
} else {
if regexp.MustCompile(`%!.?\(.+?v?\)v?$`).MatchString(expression) {
return "", nil, fmt.Errorf("format error: %s", expression)
}
if variable == "" {
b := make([]byte, 8)
rand.Read(b)
variable = "chrome" + hex.EncodeToString(b)
}
if err := chromedp.Run(ctx, chromedp.Evaluate(fmt.Sprintln("let", variable), nil)); err != nil {
return "", nil, err
}
expression = fmt.Sprintf(script, variable)
}
c := ListenEvent(ctx, url, method, download)
if err := chromedp.Run(ctx, chromedp.Evaluate(expression, nil)); err != nil {
return "", nil, err
}
return variable, c, nil
}
func ListenScript(ctx context.Context, script string, url any, method, variable string, result any) error {
ctx, cancel := context.WithCancel(ctx)
defer cancel()
variable, c, err := ListenScriptEvent(ctx, script, url, method, variable, false)
if err != nil {
return err
}
select {
case <-ctx.Done():
return ctx.Err()
case <-c:
return chromedp.Run(ctx, chromedp.Evaluate(variable, &result))
}
}
func (c *Chrome) ListenEvent(url any, method string, download bool) <-chan *Event {
return ListenEvent(c, url, method, download)
}
func (c *Chrome) ListenScriptEvent(script string, url any, method, variable string, download bool) (string, <-chan *Event, error) {
return ListenScriptEvent(c, script, url, method, variable, download)
}
func (c *Chrome) ListenScript(script string, url any, method, variable string, result any) error {
return ListenScript(c, script, url, method, variable, result)
}