-
Notifications
You must be signed in to change notification settings - Fork 241
/
Copy pathmain.go
75 lines (66 loc) · 1.72 KB
/
main.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
// Command latlon is a chromedp example demonstrating how to retrieve the
// latitude/longitude from google maps, using the browser's target events.
package main
import (
"context"
"flag"
"fmt"
"log"
"os"
"regexp"
"time"
"github.com/chromedp/cdproto/page"
"github.com/chromedp/chromedp"
)
func main() {
verbose := flag.Bool("v", false, "verbose")
timeout := flag.Duration("timeout", 1*time.Minute, "timeout")
flag.Parse()
if err := run(context.Background(), *verbose, *timeout); err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}
}
func run(ctx context.Context, verbose bool, timeout time.Duration) error {
// regexp to extract latitude, longitude
latlonRE := regexp.MustCompile(`maps/@(-?\d+\.\d+,-?\d+\.\d+),`)
// create chrome instance
var opts []chromedp.ContextOption
if verbose {
opts = append(opts, chromedp.WithDebugf(log.Printf))
}
ctx, cancel := chromedp.NewContext(ctx, opts...)
defer cancel()
// create a timeout
ctx, cancel = context.WithTimeout(ctx, timeout)
defer cancel()
// listen for the navigated event
ch, errch := make(chan string, 1), make(chan error, 1)
chromedp.ListenTarget(ctx, func(ev interface{}) {
if verbose {
log.Printf("%T: %+v\n", ev, ev)
}
switch event := ev.(type) {
case *page.EventNavigatedWithinDocument:
if m := latlonRE.FindStringSubmatch(event.URL); m != nil {
ch <- m[1]
}
}
})
// run task
go func() {
if err := chromedp.Run(ctx, chromedp.Navigate("https://www.google.com/maps/?hl=en")); err != nil {
errch <- err
}
}()
// wait for context closed, an error, or the result
select {
case <-ctx.Done():
return ctx.Err()
case err := <-errch:
return err
case res := <-ch:
fmt.Fprintln(os.Stdout, res)
}
return nil
}