-
Notifications
You must be signed in to change notification settings - Fork 116
/
after_loaded_tasks.go
executable file
·212 lines (186 loc) · 5.95 KB
/
after_loaded_tasks.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
package engine
import (
"context"
"fmt"
"github.com/yhy0/Jie/crawler/crawlergo/config"
"github.com/yhy0/Jie/crawler/crawlergo/js"
"github.com/yhy0/Jie/crawler/crawlergo/tools"
"github.com/yhy0/logging"
"time"
"github.com/chromedp/cdproto/cdp"
"github.com/chromedp/chromedp"
)
/**
根据NODE节点执行JS的代码
err := EvaluateAsDevTools(snippet(submitJS, cashX(true), sel, nodes[0]), &res).Do(ctx)
具体环境实现在 chromedp.submit 函数中 参考即可写出
*/
// AfterLoadedRun 在页面Loaded之后执行 同时等待 afterDOMRun 之后执行
func (tab *Tab) AfterLoadedRun() {
defer tab.WG.Done()
// logging.Logger.Debug("afterLoadedRun start")
tab.formSubmitWG.Add(2)
tab.loadedWG.Add(3)
tab.removeLis.Add(1)
go tab.formSubmit()
tab.formSubmitWG.Wait()
// logging.Logger.Debug("formSubmit end")
if tab.config.EventTriggerMode == config.EventTriggerAsync {
go tab.triggerJavascriptProtocol()
go tab.triggerInlineEvents()
go tab.triggerDom2Events()
tab.loadedWG.Wait()
} else if tab.config.EventTriggerMode == config.EventTriggerSync {
tab.triggerInlineEvents()
time.Sleep(tab.config.EventTriggerInterval)
tab.triggerDom2Events()
time.Sleep(tab.config.EventTriggerInterval)
tab.triggerJavascriptProtocol()
}
// 事件触发之后 需要等待一点时间让浏览器成功发出ajax请求 更新DOM
time.Sleep(tab.config.BeforeExitDelay)
go tab.RemoveDOMListener()
tab.removeLis.Wait()
// logging.Logger.Debug("afterLoadedRun end")
}
/*
*
自动化点击提交表单
*/
func (tab *Tab) formSubmit() {
// logging.Logger.Debug("formSubmit start")
// 首先对form表单设置target
tab.setFormToFrame()
// 接下来尝试三种方式提交表单
go tab.clickSubmit()
go tab.clickAllButton()
}
/*
*
设置form的target指向一个frame
*/
func (tab *Tab) setFormToFrame() {
// 首先新建 frame
nameStr := tools.RandSeq(8)
tab.Evaluate(fmt.Sprintf(js.NewFrameTemplate, nameStr, nameStr))
// 接下来将所有的 form 节点target都指向它
ctx := tab.GetExecutor()
formNodes, formErr := tab.GetNodeIDs(`form`)
if formErr != nil || len(formNodes) == 0 {
// logging.Logger.Debug("setFormToFrame: get form element err")
if formErr != nil {
// logging.Logger.Debug(formErr)
}
return
}
tCtx, cancel := context.WithTimeout(ctx, time.Second*2)
defer cancel()
_ = chromedp.SetAttributeValue(formNodes, "target", nameStr, chromedp.ByNodeID).Do(tCtx)
}
/*
*
点击按钮 type=submit
*/
func (tab *Tab) clickSubmit() {
defer tab.formSubmitWG.Done()
// 首先点击按钮 type=submit
ctx := tab.GetExecutor()
// 获取所有的form节点 直接执行submit
formNodes, formErr := tab.GetNodeIDs(`form`)
if formErr != nil || len(formNodes) == 0 {
// logging.Logger.Debug("clickSubmit: get form element err")
if formErr != nil {
logging.Logger.Debug(formErr)
}
return
}
tCtx1, cancel1 := context.WithTimeout(ctx, time.Second*2)
defer cancel1()
_ = chromedp.Submit(formNodes, chromedp.ByNodeID).Do(tCtx1)
// 获取所有的input标签
inputNodes, inputErr := tab.GetNodeIDs(`form input[type=submit]`)
if inputErr != nil || len(inputNodes) == 0 {
// logging.Logger.Debug("clickSubmit: get form input element err")
if inputErr != nil {
logging.Logger.Debug(inputErr)
}
return
}
tCtx2, cancel2 := context.WithTimeout(ctx, time.Second*2)
defer cancel2()
_ = chromedp.Click(inputNodes, chromedp.ByNodeID).Do(tCtx2)
}
/*
*
click all button
*/
func (tab *Tab) clickAllButton() {
defer tab.formSubmitWG.Done()
// 获取所有的form中的button节点
ctx := tab.GetExecutor()
// 获取所有的button标签
btnNodeIDs, bErr := tab.GetNodeIDs(`form button`)
if bErr != nil || len(btnNodeIDs) == 0 {
// logging.Logger.Debug("clickAllButton: get form button element err")
if bErr != nil {
logging.Logger.Debug(bErr)
}
return
}
tCtx, cancel1 := context.WithTimeout(ctx, time.Second*2)
defer cancel1()
_ = chromedp.Click(btnNodeIDs, chromedp.ByNodeID).Do(tCtx)
// 使用JS的click方法进行点击
var btnNodes []*cdp.Node
tCtx2, cancel2 := context.WithTimeout(ctx, time.Second*2)
defer cancel2()
err := chromedp.Nodes(btnNodeIDs, &btnNodes, chromedp.ByNodeID).Do(tCtx2)
if err != nil {
return
}
for _, node := range btnNodes {
_ = tab.EvaluateWithNode(js.FormNodeClickJS, node)
}
}
/*
*
触发内联事件
*/
func (tab *Tab) triggerInlineEvents() {
defer tab.loadedWG.Done()
// logging.Logger.Debug("triggerInlineEvents start")
tab.Evaluate(fmt.Sprintf(js.TriggerInlineEventJS, tab.config.EventTriggerInterval.Seconds()*1000))
// logging.Logger.Debug("triggerInlineEvents end")
}
/*
*
触发DOM2级事件
*/
func (tab *Tab) triggerDom2Events() {
defer tab.loadedWG.Done()
// logging.Logger.Debug("triggerDom2Events start")
tab.Evaluate(fmt.Sprintf(js.TriggerDom2EventJS, tab.config.EventTriggerInterval.Seconds()*1000))
// logging.Logger.Debug("triggerDom2Events end")
}
/*
*
a标签的href值为伪协议,
*/
func (tab *Tab) triggerJavascriptProtocol() {
defer tab.loadedWG.Done()
// logging.Logger.Debug("clickATagJavascriptProtocol start")
tab.Evaluate(fmt.Sprintf(js.TriggerJavascriptProtocol, tab.config.EventTriggerInterval.Seconds()*1000,
tab.config.EventTriggerInterval.Seconds()*1000))
// logging.Logger.Debug("clickATagJavascriptProtocol end")
}
/*
*
移除DOM节点变化监听
*/
func (tab *Tab) RemoveDOMListener() {
defer tab.removeLis.Done()
// logging.Logger.Debug("RemoveDOMListener start")
// 移除DOM节点变化监听
tab.Evaluate(js.RemoveDOMListenerJS)
// logging.Logger.Debug("RemoveDOMListener end")
}