-
Notifications
You must be signed in to change notification settings - Fork 3
/
chrome.go
169 lines (139 loc) · 3.74 KB
/
chrome.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
package svg2png
import (
"context"
"errors"
"fmt"
"net/url"
"os"
"os/exec"
"path"
"regexp"
"strings"
"time"
gover "github.com/mcuadros/go-version"
"github.com/sirupsen/logrus"
)
const (
// MinChromeVersion minimize version allowed chrome headless
MinChromeVersion = "59"
)
var (
// DefaultChromPaths posible chrome paths
DefaultChromPaths = []string{
"/usr/bin/chromium-browser",
"/usr/bin/chromium",
"/usr/bin/google-chrome-stable",
"/usr/bin/google-chrome",
"/Applications/Google Chrome.app/Contents/MacOS/Google Chrome",
"/Applications/Google Chrome Canary.app/Contents/MacOS/Google Chrome Canary",
"/Applications/Chromium.app/Contents/MacOS/Chromium",
"C:/Program Files (x86)/Google/Chrome/Application/chrome.exe"}
// DefaultChromeHeight default height value of chrome brower
DefaultChromeHeight = 1200
// DefaultChromeWidth default width value of chrome brower
DefaultChromeWidth = 720
// DefaultTimeout default timeout
DefaultTimeout = 3 * time.Second
)
// Chrome contains information about a Google Chrome
// instance, with methods to run on it.
type Chrome struct {
path string
version string
height int
width int
timeout time.Duration
}
// NewChrome intalizes new Chrome
func NewChrome() *Chrome {
path := getChromePath()
if path == "" {
logrus.Fatal("Chrome not found")
}
version := getChromeVersion(path)
if gover.Compare(MinChromeVersion, version, ">") {
logrus.Fatal("Chrome version must greater than 59")
}
logrus.Infof("Chrome version %s", version)
return &Chrome{
width: DefaultChromeWidth,
height: DefaultChromeHeight,
path: path,
version: version,
timeout: DefaultTimeout,
}
}
// SetWith sets chrome's width
func (chrome *Chrome) SetWith(w int) *Chrome {
chrome.width = w
return chrome
}
// SetHeight sets chrome's width
func (chrome *Chrome) SetHeight(h int) *Chrome {
chrome.height = h
return chrome
}
//Resolution returns chrome's resolution
func (chrome Chrome) Resolution() string {
return fmt.Sprintf("%d,%d", chrome.width, chrome.height)
}
//SetTimeout set chrome's timeout
func (chrome *Chrome) SetTimeout(timeout time.Duration) *Chrome {
chrome.timeout = timeout
return chrome
}
func getChromePath() string {
for _, path := range DefaultChromPaths {
if _, err := os.Stat(path); !os.IsNotExist(err) {
return path
}
}
return ""
}
// Version returns Chrome version
func getChromeVersion(path string) string {
out, err := exec.Command(path, "-version").Output()
if err != nil {
logrus.Fatal(err)
}
match := regexp.MustCompile(`\d+(\.\d+)+`).FindStringSubmatch(string(out))
if len(match) <= 0 {
logrus.Fatal("Unable to determine Chrome version.")
}
return match[0]
}
func isValidDestination(destination string) bool {
return path.Base(destination) != "." && path.Base(destination) != "/" && strings.EqualFold(path.Ext(destination), ".png")
}
// Screenshoot takes a screenshot by the given website url
func (chrome Chrome) Screenshoot(websiteURL string, destination string) error {
if _, err := url.Parse(websiteURL); err != nil {
return err
}
if !isValidDestination(destination) {
return errors.New("Destination must be a png path")
}
args := []string{
"--headless",
"--no-sandbox",
"--disable-crash-reporter",
"--hide-scrollbars",
"--default-background-color=00000000",
"--disable-gpu",
"--window-size=" + chrome.Resolution(),
"--screenshot=" + destination,
websiteURL,
}
ctx, cancel := context.WithTimeout(context.TODO(), chrome.timeout)
defer cancel()
if err := exec.CommandContext(ctx, chrome.path, args...).Run(); err != nil {
if ctx.Err() == context.DeadlineExceeded {
return errors.New("Takes screenshoot got timeout")
}
return err
}
if _, err := os.Stat(destination); !os.IsNotExist(err) {
return err
}
return nil
}