| skipped 3 lines |
4 | 4 | | "context" |
5 | 5 | | "crypto/tls" |
6 | 6 | | "fmt" |
| 7 | + | "log" |
| 8 | + | "os" |
| 9 | + | "path" |
| 10 | + | "path/filepath" |
7 | 11 | | "strconv" |
8 | 12 | | "strings" |
9 | 13 | | "time" |
| skipped 4 lines |
14 | 18 | | |
15 | 19 | | // RequestWithChrome Do request with real browser |
16 | 20 | | func RequestWithChrome(url string, contentID string) string { |
17 | | - | // opts := append(chromedp.DefaultExecAllocatorOptions[:], |
18 | | - | // chromedp.Flag("headless", false), |
19 | | - | // chromedp.Flag("disable-gpu", true), |
20 | | - | // chromedp.Flag("enable-automation", true), |
21 | | - | // chromedp.Flag("disable-extensions", false), |
22 | | - | // ) |
23 | | - | ctx, cancel := chromedp.NewContext(context.Background()) |
| 21 | + | // prepare the chrome options |
| 22 | + | opts := append(chromedp.DefaultExecAllocatorOptions[:], |
| 23 | + | chromedp.Flag("headless", true), |
| 24 | + | chromedp.Flag("ignore-certificate-errors", true), |
| 25 | + | chromedp.Flag("disable-gpu", true), |
| 26 | + | chromedp.Flag("enable-automation", true), |
| 27 | + | chromedp.Flag("disable-extensions", true), |
| 28 | + | chromedp.Flag("disable-setuid-sandbox", true), |
| 29 | + | chromedp.Flag("disable-web-security", true), |
| 30 | + | chromedp.Flag("no-first-run", true), |
| 31 | + | chromedp.Flag("no-default-browser-check", true), |
| 32 | + | ) |
| 33 | + | |
| 34 | + | allocCtx, bcancel := chromedp.NewExecAllocator(context.Background(), opts...) |
| 35 | + | allocCtx, bcancel = context.WithTimeout(allocCtx, 10*time.Second) |
| 36 | + | defer bcancel() |
| 37 | + | |
| 38 | + | ctx, cancel := chromedp.NewContext(allocCtx, chromedp.WithLogf(log.Printf)) |
24 | 39 | | ctx, cancel = context.WithTimeout(ctx, 10*time.Second) |
25 | 40 | | defer cancel() |
26 | 41 | | |
| skipped 1 lines |
28 | 43 | | var data string |
29 | 44 | | err := chromedp.Run(ctx, |
30 | 45 | | chromedp.Navigate(url), |
31 | | - | // chromedp.WaitVisible(contentID), |
32 | 46 | | chromedp.OuterHTML(contentID, &data, chromedp.NodeVisible, chromedp.ByID), |
33 | 47 | | ) |
| 48 | + | |
| 49 | + | // clean chromedp-runner folder |
| 50 | + | cleanUp() |
| 51 | + | |
34 | 52 | | if err != nil { |
35 | 53 | | return "" |
36 | 54 | | } |
| 55 | + | return data |
| 56 | + | } |
37 | 57 | | |
38 | | - | return data |
| 58 | + | func cleanUp() { |
| 59 | + | tmpFolder := path.Join(os.TempDir(), "chromedp-runner*") |
| 60 | + | if _, err := os.Stat("/tmp/"); !os.IsNotExist(err) { |
| 61 | + | tmpFolder = path.Join("/tmp/", "chromedp-runner*") |
| 62 | + | } |
| 63 | + | junks, err := filepath.Glob(tmpFolder) |
| 64 | + | if err != nil { |
| 65 | + | return |
| 66 | + | } |
| 67 | + | for _, junk := range junks { |
| 68 | + | os.RemoveAll(junk) |
| 69 | + | } |
39 | 70 | | } |
40 | 71 | | |
41 | 72 | | // SendGET just send GET request |
| skipped 115 lines |