forked from Ecwid/gosnap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
runtime.go
192 lines (174 loc) · 6.72 KB
/
runtime.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package cdp
import (
"fmt"
)
// ExecutionContextDescription https://chromedevtools.github.io/devtools-protocol/tot/Runtime#type-ExecutionContextDescription
type ExecutionContextDescription struct {
ID int64 `json:"id"`
Origin string `json:"origin"`
Name string `json:"name"`
AuxData map[string]interface{} `json:"auxData"`
}
// RemoteObject https://chromedevtools.github.io/devtools-protocol/tot/Runtime#type-RemoteObject
type RemoteObject struct {
Type string `json:"type"`
Subtype string `json:"subtype"`
ClassName string `json:"className"`
Value interface{} `json:"value"`
UnserializableValue string `json:"unserializableValue"`
Description string `json:"description"`
ObjectID string `json:"objectId"`
}
// ExceptionDetails https://chromedevtools.github.io/devtools-protocol/tot/Runtime#type-ExceptionDetails
type ExceptionDetails struct {
ExceptionID int64 `json:"exceptionId"`
Text string `json:"text"`
LineNumber int64 `json:"lineNumber"`
ColumnNumber int64 `json:"columnNumber"`
ScriptID string `json:"scriptId"`
URL string `json:"url"`
Exception *RemoteObject `json:"exception"`
ExecutionContextID int64 `json:"executionContextId"`
}
// PropertyDescriptor https://chromedevtools.github.io/devtools-protocol/tot/Runtime#type-PropertyDescriptor
type PropertyDescriptor struct {
Name string `json:"name"`
Value RemoteObject `json:"value"`
Writable bool `json:"writable"`
Get *RemoteObject `json:"get"`
Set *RemoteObject `json:"set"`
Configurable bool `json:"configurable"`
Enumerable bool `json:"enumerable"`
WasThrown bool `json:"wasThrown"`
IsOwn bool `json:"isOwn"`
Symbol *RemoteObject `json:"symbol"`
}
// CallArgument https://chromedevtools.github.io/devtools-protocol/tot/Runtime#type-CallArgument
type CallArgument struct {
Value interface{} `json:"value,omitempty"`
UnserializableValue string `json:"unserializableValue,omitempty"`
ObjectID string `json:"objectId,omitempty"`
}
// EvaluatesExpression https://chromedevtools.github.io/devtools-protocol/tot/Runtime#method-evaluate
type EvaluatesExpression struct {
Expression string `json:"expression"`
ObjectGroup string `json:"objectGroup,omitempty"`
IncludeCommandLineAPI bool `json:"includeCommandLineAPI,omitempty"`
Silent bool `json:"silent,omitempty"`
ContextID int64 `json:"contextId,omitempty"`
ReturnByValue bool `json:"returnByValue,omitempty"`
GeneratePreview bool `json:"generatePreview,omitempty"`
UserGesture bool `json:"userGesture,omitempty"`
AwaitPromise bool `json:"awaitPromise,omitempty"`
ThrowOnSideEffect bool `json:"throwOnSideEffect,omitempty"`
Timeout int64 `json:"timeout,omitempty"`
}
// StackTrace https://chromedevtools.github.io/devtools-protocol/tot/Runtime#type-StackTrace
type StackTrace struct {
Description string `json:"description"`
CallFrames []*CallFrame `json:"callFrames"`
Parent *StackTrace `json:"parent"`
ParentID *StackTraceID `json:"parentId"`
}
// CallFrame https://chromedevtools.github.io/devtools-protocol/tot/Runtime#type-CallFrame
type CallFrame struct {
FunctionName string `json:"functionName"`
ScriptID string `json:"scriptId"`
URL string `json:"url"`
LineNumber int64 `json:"lineNumber"`
ColumnNumber int64 `json:"columnNumber"`
}
// StackTraceID https://chromedevtools.github.io/devtools-protocol/tot/Runtime#type-StackTraceId
type StackTraceID struct {
ID string `json:"id"`
DebuggerID string `json:"debuggerId"`
}
// ConsoleAPICalled https://chromedevtools.github.io/devtools-protocol/tot/Runtime#event-consoleAPICalled
type ConsoleAPICalled struct {
Type string `json:"type"`
Args []*RemoteObject `json:"args"`
ExecutionContextID int64 `json:"executionContextId"`
Timestamp float64 `json:"timestamp"`
StackTrace *StackTrace `json:"stackTrace"`
Context string `json:"context"`
}
func (r *RemoteObject) bool() bool {
return r.Type == "boolean" && r.Value.(bool)
}
func (e *ExceptionDetails) Error() string {
return fmt.Sprintf("%+v", e.Exception)
}
func (session *Session) getExceptionDetails(msg MessageResult) error {
if exceptionDetails, has := msg["exceptionDetails"]; has {
details := &ExceptionDetails{}
unmarshal(exceptionDetails, details)
return details
}
return nil
}
// https://chromedevtools.github.io/devtools-protocol/tot/Runtime#method-releaseObject
func (session *Session) releaseObject(objectID string) error {
_, err := session.blockingSend("Runtime.releaseObject", &Params{"objectId": objectID})
return err
}
// Evaluate Evaluates expression on global object.
func (session *Session) Evaluate(expression string, contextID int64) (*RemoteObject, error) {
exp := &EvaluatesExpression{
Expression: expression,
ContextID: contextID,
AwaitPromise: true,
ReturnByValue: false,
}
params := &Params{}
unmarshal(exp, params)
res, err := session.blockingSend("Runtime.evaluate", params)
if err != nil {
return nil, err
}
if err := session.getExceptionDetails(res); err != nil {
return nil, err
}
obj := &RemoteObject{}
unmarshal(res["result"], obj)
return obj, nil
}
func (session *Session) getProperties(objectID string) ([]*PropertyDescriptor, error) {
evaluated, err := session.blockingSend("Runtime.getProperties", &Params{
"objectId": objectID,
"ownProperties": true,
"accessorPropertiesOnly": false,
})
if err != nil {
return nil, err
}
err = session.getExceptionDetails(evaluated)
if err != nil {
return nil, err
}
obj := make([]*PropertyDescriptor, 0)
unmarshal(evaluated["result"], &obj)
return obj, nil
}
func (session *Session) callFunctionOn(objectID string, functionDeclaration string, arg ...interface{}) (*RemoteObject, error) {
args := make([]CallArgument, len(arg))
for i, a := range arg {
args[i] = CallArgument{Value: a}
}
evaluated, err := session.blockingSend("Runtime.callFunctionOn", &Params{
"functionDeclaration": functionDeclaration,
"objectId": objectID,
"arguments": args,
"awaitPromise": true,
"returnByValue": false,
})
if err != nil {
return nil, err
}
err = session.getExceptionDetails(evaluated)
if err != nil {
return nil, err
}
obj := &RemoteObject{}
unmarshal(evaluated["result"], obj)
return obj, nil
}