-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutil.go
82 lines (62 loc) · 1.72 KB
/
util.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
76
77
78
79
80
81
82
package chromy
import (
"context"
"encoding/json"
"github.com/mafredri/cdp"
"github.com/mafredri/cdp/protocol/dom"
"github.com/mafredri/cdp/protocol/runtime"
)
type CallOption func(*runtime.CallFunctionOnArgs)
func nodeIDToRemoteObjectID(ctx context.Context, cli *cdp.Client, nodeID dom.NodeID) (runtime.RemoteObjectID, error) {
reply, err := cli.DOM.ResolveNode(ctx, dom.NewResolveNodeArgs().SetNodeID(nodeID))
if err != nil {
return "", err
}
if reply.Object.ObjectID == nil {
return "", ErrUnableToResolveNode
}
return *reply.Object.ObjectID, nil
}
func callFuncOnRemoteObject(ctx context.Context, cli *cdp.Client, objectID runtime.RemoteObjectID, declaration string, arguments []interface{}, res interface{}, opt ...CallOption) error {
callArgs := make([]runtime.CallArgument, 0, len(arguments))
for _, one := range arguments {
callArg := runtime.CallArgument{}
switch v := one.(type) {
case *runtime.RemoteObject:
callArg.ObjectID = v.ObjectID
case runtime.RemoteObject:
callArg.ObjectID = v.ObjectID
case *runtime.RemoteObjectID:
callArg.ObjectID = v
case runtime.RemoteObjectID:
callArg.ObjectID = &v
default:
b, err := json.Marshal(v)
if err != nil {
return err
}
callArg.Value = json.RawMessage(b)
}
callArgs = append(callArgs, callArg)
}
arg := runtime.NewCallFunctionOnArgs(declaration).
SetObjectID(objectID).
SetArguments(callArgs)
if res != nil {
arg.SetReturnByValue(true)
}
reply, err := cli.Runtime.CallFunctionOn(ctx, arg)
if err != nil {
return err
}
if reply.ExceptionDetails != nil {
return reply.ExceptionDetails
}
if res != nil {
err = json.Unmarshal(reply.Result.Value, res)
if err != nil {
return err
}
}
return nil
}