-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcapture.go
58 lines (46 loc) · 962 Bytes
/
capture.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
package main
import (
"io/ioutil"
"github.com/chromedp/chromedp"
"context"
"fmt"
"time"
)
func capture(ctxt context.Context, query string, sel string, out string) error {
var err error
// create chrome instance
c, err := chromedp.New(ctxt)
if err != nil {
return err
}
// run task list
var buf []byte
url := fmt.Sprintf("%s%s", screenConfig.DomainScope, query)
err = c.Run(ctxt, screenshot(url, sel, &buf))
if err != nil {
return err
}
// shutdown chrome
err = c.Shutdown(ctxt)
if err != nil {
return err
}
// wait for chrome to finish
err = c.Wait()
if err != nil {
return err
}
err = ioutil.WriteFile(out, buf, 0644)
if err != nil {
return err
}
return nil
}
func screenshot(urlstr, sel string, res *[]byte) chromedp.Tasks {
return chromedp.Tasks{
chromedp.Navigate(urlstr),
chromedp.Sleep(1 * time.Second),
chromedp.WaitVisible(sel, chromedp.ByID),
chromedp.Screenshot(sel, res, chromedp.ByID),
}
}