-
Notifications
You must be signed in to change notification settings - Fork 43
/
page.go
181 lines (157 loc) · 3.76 KB
/
page.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
package core
import (
"github.com/yaklang/yaklang/common/log"
"github.com/yaklang/yaklang/common/utils"
"github.com/yaklang/yaklang/common/yak/yaklib/codec"
"strings"
"time"
"github.com/go-rod/rod"
"github.com/go-rod/rod/lib/proto"
)
func (m *Manager) WaitLoad(page *rod.Page) {
page.MustWaitLoad()
//defer page.Close()
// defer page.MustClose()
// defer m.PutPage(page)
//defer m.PutPage(page)
var lastHtmlHash = ""
var exitThroshold = 0
var totalFetcher = 0
var htmlStr string
for {
totalFetcher++
if totalFetcher > 10 {
break
}
if exitThroshold >= 2 {
break
}
time.Sleep(500 * time.Millisecond)
htmlStr, err := page.HTML()
if err != nil {
log.Errorf("fetch html failed: %s", err)
}
if lastHtmlHash == "" && htmlStr == "" {
exitThroshold++
continue
}
// 有效缓存
if lastHtmlHash == "" && htmlStr != "" {
lastHtmlHash = codec.Sha256(htmlStr)
exitThroshold = 0
continue
}
// 重复三次
if lastHtmlHash == codec.Sha256(htmlStr) {
exitThroshold++
continue
} else {
exitThroshold = 0
lastHtmlHash = codec.Sha256(htmlStr)
}
}
if htmlStr == "" {
log.Error("page empty")
}
}
func (m *Manager) page(url string, depth int) error {
defer m.pageSizedWaitGroup.Done()
page_block, err := m.GetPage(proto.TargetCreateTarget{
URL: "about:blank",
}, depth)
page := page_block.page.Timeout(time.Duration(m.config.timeout) * time.Second)
// page := page_block.page
if err != nil {
return utils.Errorf("get blank page[%v] failed: %s", url, err)
}
if page == nil {
return utils.Errorf("get blank page[%v] nil: %s", url, err)
}
// 页面正常退出
// defer page.Close()
defer m.PutPage(page)
// page.Navigate(url)
err = page.Navigate(url)
if err != nil {
return utils.Errorf("navigate page[%v] failed: %s", url, err)
}
page = page.Context(m.rootContext)
// m.visitedUrl.Insert(url)
// page.MustWaitLoad()
// time.Sleep(1 * time.Second)
wait_count := 0
for {
state, err := m.GetReadyState(page)
if err != nil {
return utils.Errorf("get ready state error: %s", err)
}
if state == "complete" {
break
}
wait_count++
if wait_count > 20 {
log.Infof("wait ready state too long error: %s.", state)
break
}
time.Sleep(1 * time.Second)
}
var lastHtmlHash = ""
var exitThroshold = 0
var totalFetcher = 0
var htmlStr string
for {
totalFetcher++
if totalFetcher > 10 {
break
}
if exitThroshold >= 2 {
break
}
time.Sleep(500 * time.Millisecond)
htmlStr, err = page.HTML()
if err != nil && !strings.Contains(err.Error(), "context canceled") {
log.Errorf("fetch [%v]'s html failed: %s", url, err)
}
if lastHtmlHash == "" && htmlStr == "" {
exitThroshold++
continue
}
// 有效缓存
if lastHtmlHash == "" && htmlStr != "" {
lastHtmlHash = codec.Sha256(htmlStr)
exitThroshold = 0
continue
}
// 重复三次
if lastHtmlHash == codec.Sha256(htmlStr) {
exitThroshold++
continue
} else {
exitThroshold = 0
lastHtmlHash = codec.Sha256(htmlStr)
}
}
if htmlStr == "" {
// log.Error("page empty")
return nil
}
//存储响应到chan
// 到这里认为页面加载差不多了
// 下面要去做别的事情,比如:
// 1. 获取页面可点击的链接
// 2. 获取页面可处理的表格
// page.MustScreenshot("test.png")
err = m.extractInput(page)
if err != nil && !strings.Contains(err.Error(), "context canceled") {
log.Errorf("extract blanks and do input failed: %s", err)
}
err = m.extractUrls(page_block)
if err != nil && !strings.Contains(err.Error(), "context canceled") {
log.Errorf("extract urls failed: %s", err)
}
err = m.extractCommit(page_block)
if err != nil && !strings.Contains(err.Error(), "context canceled") {
log.Errorf("extract commit failed: %s", err)
}
return nil
}