/
etc.go
121 lines (99 loc) · 2.61 KB
/
etc.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
package katch
import (
"context"
"encoding/json"
"fmt"
"github.com/chromedp/cdproto/dom"
"github.com/chromedp/cdproto/page"
"github.com/chromedp/cdproto/runtime"
"github.com/chromedp/chromedp"
"time"
)
func extractDocumentElementScrollTop(ctx context.Context) (float64, error) {
result, exception, err := runtime.Evaluate("document.documentElement.scrollTop").Do(ctx)
if err != nil {
return 0, err
}
if exception != nil {
return 0, exception
}
var val float64
err = json.Unmarshal(result.Value, &val)
return val, err
}
func setDocumentElementScrollTop(ctx context.Context, step int64) error {
_, exception, err := runtime.Evaluate(fmt.Sprintf("document.documentElement.scrollTop += %d", step)).Do(ctx)
if err != nil {
return err
}
if exception != nil {
return exception
}
return nil
}
func sleep(ctx context.Context, d time.Duration) error {
timer := time.NewTimer(d)
select {
case <-ctx.Done():
if !timer.Stop() {
<-timer.C
}
return ctx.Err()
case <-timer.C:
return nil
}
}
func infinityScrollTask(scrollMaxTimes int64, scrollStep int64, scrollDelayDuration time.Duration) chromedp.ActionFunc {
return func(ctx context.Context) error {
reachedBottom := false
prevDocumentElementScrollTop := float64(0)
scrollTimes := int64(0)
for !reachedBottom && !(scrollMaxTimes > -1 && scrollTimes >= scrollMaxTimes) {
if err := setDocumentElementScrollTop(ctx, scrollStep); err != nil {
return err
}
if err := sleep(ctx, scrollDelayDuration); err != nil {
return err
}
newDocumentElementScrollTop, err := extractDocumentElementScrollTop(ctx)
if err != nil {
return err
}
if newDocumentElementScrollTop == prevDocumentElementScrollTop {
reachedBottom = true
}
prevDocumentElementScrollTop = newDocumentElementScrollTop
scrollTimes++
}
return nil
}
}
func pdfExporterTask(landscape bool, printBackground bool, paperHeight float64, paperWidth float64, output *[]byte) chromedp.ActionFunc {
return func(ctx context.Context) error {
pdfParams := page.PrintToPDF()
pdfParams.Landscape = landscape
pdfParams.PrintBackground = printBackground
pdfParams.PaperHeight = paperHeight
pdfParams.PaperWidth = paperWidth
buf, _, err := pdfParams.Do(ctx)
if err != nil {
return err
}
*output = buf
return nil
}
}
func htmlExporterTask(output *[]byte) chromedp.ActionFunc {
return func(ctx context.Context) error {
node, err := dom.GetDocument().Do(ctx)
if err != nil {
return err
}
outputStr, err := dom.GetOuterHTML().WithNodeID(node.NodeID).Do(ctx)
if err != nil {
return err
}
*output = []byte(outputStr)
return nil
}
}