Debian Codegraph @

lib/examples/compare-chromedp/download_file/main.go

raw
1 // Package main ...
2 package main
3
4 import (
5 "fmt"
6 "log"
7 "os"
8 "path/filepath"
9
10 "github.com/go-rod/rod"
11 "github.com/go-rod/rod/lib/proto"
12 )
13
14 func main() {
15 // get working directory
16 wd, err := os.Getwd()
17 if err != nil {
18 log.Fatal(err)
19 }
20
21 browser := rod.New().MustConnect()
22
23 page := browser.MustPage("https://github.com/chromedp/examples")
24
25 page.MustElementR("summary", "Code").MustClick()
26
27 wait := page.Browser().WaitDownload(wd)
28
29 go browser.EachEvent(func(e *proto.PageDownloadProgress) bool {
30 completed := "(unknown)"
31 if e.TotalBytes != 0 {
32 completed = fmt.Sprintf("%0.2f%%", e.ReceivedBytes/e.TotalBytes*100.0)
33 }
34 log.Printf("state: %s, completed: %s\n", e.State, completed)
35 return e.State == proto.PageDownloadProgressStateCompleted
36 })()
37
38 page.MustElementR("a", "Download ZIP").MustClick()
39
40 res := wait()
41
42 log.Printf("wrote %s", filepath.Join(wd, res.GUID))
43 }