-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
rendering.go
178 lines (142 loc) · 4.6 KB
/
rendering.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
// Package rendering provides a function to render HTML to images or HTML after JS execution.
// It uses the Chrome Debug Protocol through the rod package. It will download a headless
// Chrome version if needed that matches the current platform.
//
// If the environment variable SND_CHROME_DEBUG=1 it will start the chrome instances
// in non-headless mode.
package rendering
import (
"bytes"
"errors"
"image"
_ "image/png"
"net/url"
"os"
"runtime"
"time"
"github.com/BigJk/snd/log"
"github.com/go-rod/rod/lib/launcher"
"github.com/go-rod/rod/lib/launcher/flags"
"github.com/go-rod/rod"
"github.com/go-rod/rod/lib/proto"
)
const IdleRequestTimeout = 5
const ReadyTimeout = 5
var browser *rod.Browser
func InitBrowser() {
if browser != nil {
_ = browser.Close()
}
// if you want rod to connect to a custom chrome instance instead of downloading and
// auto connecting you can set the SND_CHROME_ADDR to the address of target instance.
// Input might look like: 2814, 127.0.0.1:2814, ws://127.0.0.1:2814, ...
//
// you can use the rod docker container:
// - https://github.com/go-rod/rod/pkgs/container/rod
// - https://github.com/go-rod/rod/blob/master/lib/examples/connect-browser/main.go#L21
customChromeUrl := os.Getenv("SND_CHROME_ADDR")
if len(customChromeUrl) > 0 {
u := launcher.MustResolveURL(customChromeUrl)
browser = rod.New().ControlURL(u).MustConnect()
return
}
l := launcher.New()
// specify a local chrome binary instead of downloading.
customChromeBinary := os.Getenv("SND_CHROME_BIN")
if len(customChromeBinary) > 0 {
l.Set(flags.Bin, customChromeBinary)
} else if os.Getenv("SND_CHROME_SKIP_LOCAL") != "1" {
// try to auto-detect chrome
if chrome, ok := launcher.LookPath(); ok {
l.Set(flags.Bin, chrome)
log.Info("local chrome found! If rendering causes problems start S&D with the environment flag SND_CHROME_SKIP_LOCAL=1", log.WithValue("bin", chrome))
} else {
log.Info("local chrome not found")
}
}
// disable leakless for now on windows (https://github.com/BigJk/snd/issues/28)
if runtime.GOOS == "windows" {
log.Info("disabling leakless on windows")
l = l.Leakless(false)
}
// if SND_CHROME_DEBUG=1 we start the chrome instance in non-headless and with opened
// devtools so that we can see and debug the rendering that happens.
if os.Getenv("SND_CHROME_DEBUG") == "1" {
l = l.Headless(false).Devtools(true)
}
browser = rod.New().ControlURL(l.MustLaunch()).MustConnect()
}
func Shutdown() error {
return browser.Close()
}
func tryOpenPage(url string) (*rod.Page, error) {
var page *rod.Page
var err error
for i := 0; i < 2; i++ {
page, err = browser.Page(proto.TargetCreateTarget{
URL: url,
})
if err == nil {
return page, nil
}
// if the pc has gone into standby the cdp session closes,
// so we try to init it again and try once more.
InitBrowser()
}
// after retry return last error
return nil, err
}
func screenshotPage(page *rod.Page, width int) (image.Image, error) {
imageData, err := page.MustSetViewport(width, 10000, 1.0, false).MustElement("body").Screenshot(proto.PageCaptureScreenshotFormatPng, 100)
if err != nil {
return nil, err
}
img, _, err := image.Decode(bytes.NewBuffer(imageData))
if err != nil {
return nil, err
}
if img.Bounds().Max.Y >= 9500 {
return nil, errors.New("too large")
}
return img, nil
}
// RenderHTML renders the element #content into an image.
func RenderHTML(html string, width int) (image.Image, error) {
page, err := tryOpenPage("data:text/html," + url.PathEscape(html))
if err != nil {
return nil, err
}
page.MustWaitLoad().MustWaitIdle()
page.Timeout(time.Second * IdleRequestTimeout).MustWaitRequestIdle()()
defer page.Close()
return screenshotPage(page, width)
}
// RenderURL opens the URL and renders the element #content into an image.
func RenderURL(url string, width int) (image.Image, error) {
page, err := tryOpenPage(url)
if err != nil {
return nil, err
}
page.MustWaitLoad().MustWaitIdle()
page.Timeout(time.Second * IdleRequestTimeout).MustWaitRequestIdle()()
defer page.Close()
return screenshotPage(page, width)
}
// ExtractHTML opens the URL, lets the page executes and returns the HTML.
func ExtractHTML(url string, selector string) (string, error) {
page, err := tryOpenPage(url)
if err != nil {
return "", err
}
page.MustWaitLoad().MustWaitIdle()
page.Timeout(time.Second * IdleRequestTimeout).MustWaitRequestIdle()()
defer page.Close()
if err := page.Timeout(time.Second*ReadyTimeout).WaitElementsMoreThan(selector, 0); err != nil {
return "", err
}
sel, err := page.Element(selector)
if err != nil {
return "", err
}
return sel.HTML()
}