-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
177 lines (142 loc) · 4.84 KB
/
main.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
package main
import (
"context"
"flag"
"os"
"time"
log "github.com/sirupsen/logrus"
"github.com/chromedp/chromedp"
"github.com/chromedp/cdproto/cdp"
"github.com/chromedp/cdproto/network"
"github.com/chromedp/cdproto/runtime"
)
var MyExecAllocatorOptions = [...]chromedp.ExecAllocatorOption{
chromedp.NoFirstRun,
chromedp.NoDefaultBrowserCheck,
// After Puppeteer's default behavior.
chromedp.Flag("disable-background-networking", true),
chromedp.Flag("enable-features", "NetworkService,NetworkServiceInProcess"),
chromedp.Flag("disable-background-timer-throttling", true),
chromedp.Flag("disable-backgrounding-occluded-windows", true),
chromedp.Flag("disable-breakpad", true),
chromedp.Flag("disable-client-side-phishing-detection", true),
chromedp.Flag("disable-default-apps", true),
chromedp.Flag("disable-dev-shm-usage", true),
chromedp.Flag("disable-extensions", true),
chromedp.Flag("disable-features", "site-per-process,Translate,BlinkGenPropertyTrees"),
chromedp.Flag("disable-hang-monitor", true),
chromedp.Flag("disable-ipc-flooding-protection", true),
chromedp.Flag("disable-popup-blocking", true),
chromedp.Flag("disable-prompt-on-repost", true),
chromedp.Flag("disable-renderer-backgrounding", true),
chromedp.Flag("disable-sync", true),
chromedp.Flag("force-color-profile", "srgb"),
chromedp.Flag("metrics-recording-only", true),
chromedp.Flag("safebrowsing-disable-auto-update", true),
chromedp.Flag("enable-automation", true),
chromedp.Flag("password-store", "basic"),
chromedp.Flag("use-mock-keychain", true),
}
func main() {
url := flag.String("url", "", "URL to screenshot")
chromePath := flag.String("chrome-path", "", "Path to chrome or headless-shell executable")
width := flag.Int("width", 850, "Viewport width")
height := flag.Int("height", 1000, "Viewport height")
quality := flag.Int("quality", 95, "PNG quality (0-100)")
timeoutMilliseconds := flag.Int("timeout-ms", 0, "Timeout in milliseconds. Pass 0 to use no timeout.")
cookieName := flag.String("cookieName", "", "Cookie name")
cookieValue := flag.String("cookieValue", "", "Cookie value")
cookieDomain := flag.String("cookieDomain", "", "Cookie domain")
debug := flag.Bool("debug", false, "Enable debug output")
debugChrome := flag.Bool("debug-chrome", false, "Enable Chrome debug output")
flag.Parse()
if *debug {
log.SetLevel(log.DebugLevel)
}
if *url == "" {
log.Fatal("-url is required")
}
options := []chromedp.ExecAllocatorOption{}
options = append(options, MyExecAllocatorOptions[:]...)
options = append(options, chromedp.DisableGPU)
options = append(options, chromedp.WindowSize(*width, *height))
if *chromePath != "" {
options = append(options, chromedp.ExecPath(*chromePath))
}
actx, acancel := chromedp.NewExecAllocator(context.Background(), options...)
defer acancel()
var ctx context.Context
var cancel context.CancelFunc
if *debugChrome {
ctx, cancel = chromedp.NewContext(actx, chromedp.WithDebugf(log.Printf))
} else {
ctx, cancel = chromedp.NewContext(actx)
}
defer cancel()
var previewRes bool
var buf []byte
if err := chromedp.Run(ctx, fullScreenshot(
*cookieName, *cookieValue, *cookieDomain,
*url,
&previewRes,
*quality,
*timeoutMilliseconds,
&buf,
)); err != nil {
log.Fatal(err)
}
if err := os.WriteFile("screenshot.png", buf, 0o644); err != nil {
log.Fatal(err)
}
log.Printf("Wrote screenshot.png")
}
func fullScreenshot(
cookieName string,
cookieValue string,
cookieDomain string,
urlstr string,
previewRes *bool,
quality int,
timeoutMilliseconds int,
res *[]byte,
) chromedp.Tasks {
var actions chromedp.Tasks
if cookieName != "" && cookieValue != "" {
actions = append(actions, chromedp.ActionFunc(func(ctx context.Context) error {
expires := cdp.TimeSinceEpoch(time.Now().Add(180 * 24 * time.Hour))
log.Debug("Setting cookie")
err := network.SetCookie(cookieName, cookieValue).
WithExpires(&expires).
WithDomain(cookieDomain).
WithHTTPOnly(true).
Do(ctx)
if err != nil {
return err
}
return nil
}))
}
actions = append(actions, chromedp.ActionFunc(func(ctx context.Context) error {
log.Debug("Navigating")
return nil
}))
actions = append(actions, chromedp.Navigate(urlstr))
actions = append(actions, chromedp.ActionFunc(func(ctx context.Context) error {
log.Debug("Waiting for previewReady promise")
return nil
}))
actions = append(actions, chromedp.Evaluate(`window["previewReady"];`, &previewRes, func(p *runtime.EvaluateParams) *runtime.EvaluateParams {
var ret = p.WithAwaitPromise(true)
if timeoutMilliseconds > 0 {
return ret.WithTimeout(runtime.TimeDelta((timeoutMilliseconds)))
} else {
return ret
}
}))
actions = append(actions, chromedp.ActionFunc(func(ctx context.Context) error {
log.Debug("Taking screenshot")
return nil
}))
actions = append(actions, chromedp.FullScreenshot(res, quality))
return actions
}