-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathchrome.go
44 lines (35 loc) · 1.09 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
package emailscraper
import (
"context"
"fmt"
"time"
"github.com/chromedp/chromedp"
"github.com/gocolly/colly/v2"
)
func initiateScrapingFromChrome(response *colly.Response, timeout int) error {
opts := []chromedp.ExecAllocatorOption{
//nolint:lll // allow longer line here
chromedp.UserAgent("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3830.0 Safari/537.36"),
//nolint:gomnd // allow magic number here
chromedp.WindowSize(1920, 1080),
chromedp.NoFirstRun,
chromedp.Headless,
chromedp.DisableGPU,
}
ctx, cancel := chromedp.NewExecAllocator(context.Background(), opts...)
defer cancel()
ctx, cancel = chromedp.NewContext(ctx)
defer cancel()
if timeout > 0 {
ctx, cancel = context.WithTimeout(ctx, time.Duration(timeout)*time.Second)
defer cancel()
}
var res string
if err := chromedp.Run(ctx, chromedp.Navigate(response.Request.URL.String()),
chromedp.InnerHTML("html", &res), // Scrape whole rendered page
); err != nil {
return fmt.Errorf("executing chromedp: %w", err)
}
response.Body = []byte(res)
return nil
}