@@ -11,76 +11,65 @@ _Warning_ This is an experimental project, backward compatibility is not guarant
11
11
Here is an example of using:
12
12
13
13
``` go
14
- package main
15
-
16
- import (
17
- " context"
18
- " log"
19
- " time"
20
-
21
- " github.com/ecwid/control"
22
- " github.com/ecwid/control/chrome"
23
- )
24
-
25
14
func main () {
26
- chromium , err := chrome.Launch (context.TODO (), " --disable-popup-blocking" ) // you can specify more startup parameters for chrome
27
- if err != nil {
28
- panic (err)
29
- }
30
- defer chromium.Close ()
31
- ctrl := control.New (chromium.GetClient ())
32
-
33
- session , err := ctrl.CreatePageTarget (" " )
15
+ session , cancel , err := control.Take (" --no-startup-window" )
34
16
if err != nil {
35
17
panic (err)
36
18
}
19
+ defer cancel ()
37
20
38
- var page = session.Page () // main frame
39
- err = page.Navigate (" https://surfparadise.ecwid.com/" , control.LifecycleIdleNetwork , time.Second *60 )
40
- if err != nil {
41
- panic (err)
21
+ retrier := retry.Static {
22
+ Timeout: 10 * time.Second ,
23
+ Delay: 500 * time.Millisecond , // delay between attempts
42
24
}
43
25
44
- items , err := page.QuerySelectorAll (" .grid-product__title-inner" )
26
+ session.Frame .MustNavigate (" https://zoid.ecwid.com" )
27
+
28
+ var products []string
29
+ err = retry.Func (retrier, func () error {
30
+ products = []string {}
31
+ return session.Frame .QueryAll (" .grid-product__title-inner" ).Then (func (nl control.NodeList ) error {
32
+ return nl.Foreach (func (n *control.Node ) error {
33
+ return n.GetText ().Then (func (s string ) error {
34
+ products = append (products, s)
35
+ return nil
36
+ })
37
+ })
38
+ })
39
+ })
45
40
if err != nil {
46
41
panic (err)
47
42
}
48
- for _ , i := range items {
49
- title , err := i.GetText ()
50
- if err != nil {
51
- panic (err)
52
- }
53
- log.Print (title)
43
+
44
+ // "must way" throws panic on an error
45
+
46
+ for _ , node := range session.Frame .MustQueryAll (" .grid-product__title-inner" ) {
47
+ log.Println (node.MustGetText ())
54
48
}
55
49
}
56
50
```
57
51
58
52
You can call any CDP method implemented in protocol package using a session
59
53
``` go
60
- err = security.SetIgnoreCertificateErrors (sess , security.SetIgnoreCertificateErrorsArgs {
54
+ err = security.SetIgnoreCertificateErrors (session , security.SetIgnoreCertificateErrorsArgs {
61
55
Ignore : true ,
62
56
})
63
57
```
64
58
65
- or even call a custom method
59
+ or call a custom unimplemented method
66
60
``` go
67
- err = sess .Call (" Security.setIgnoreCertificateErrors" , sendStruct, receiveStruct)
61
+ err = session .Call (" Security.setIgnoreCertificateErrors" , sendStruct, receiveStruct)
68
62
```
69
63
70
- Subscribe on domain event
64
+ Subscribe on the domain event
71
65
``` go
72
- cancel := sess.Subscribe (" Overlay.screenshotRequested" , func (e observe.Value ) {
73
- v := overlay.ScreenshotRequested {}
74
- _= json.Unmarshal (e.Params , &v)
75
- doSomething (v.Viewport .Height )
66
+ future := control.Subscribe (session, " Target.targetCreated" , func (t target.TargetCreated ) bool {
67
+ return t.TargetInfo .Type == " page"
76
68
})
77
- defer cancel ()
69
+ defer future. Cancel ()
78
70
79
- // Subscribe on all incoming events
80
- sess.Subscribe (" *" , func (e observe.Value ) {
81
- switch e.Method {
82
- case " Overlay.screenshotRequested" :
83
- }
84
- })
71
+ // do something here ...
85
72
86
- ```
73
+ ctx , cancel := context.WithTimeout (context.TODO (), time.Second *10 )
74
+ result /* target.TargetCreated */ , err := future.Get (ctx)
75
+ ```
0 commit comments