forked from TommyLeng/go-rod
-
Notifications
You must be signed in to change notification settings - Fork 0
/
patch.go
79 lines (68 loc) · 2.47 KB
/
patch.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
package main
import (
"fmt"
"github.com/ysmood/gson"
)
func patch(json gson.JSON) {
k := func(k, v string) gson.Query {
return func(obj interface{}) (val interface{}, has bool) {
for _, el := range obj.([]interface{}) {
res := el.(map[string]interface{})[k]
if res == v {
return el, true
}
}
panic("not found")
}
}
index := func(obj interface{}, k, v string) string {
for i, el := range obj.([]interface{}) {
res := el.(map[string]interface{})[k]
if res == v {
return fmt.Sprintf("%d", i)
}
}
panic("not found")
}
getTypes := func(domain string) gson.JSON {
res, _ := json.Gets("domains", k("domain", domain), "types")
return res
}
// TargetTargetInfoType
j, _ := getTypes("Target").Gets(k("id", "TargetInfo"), "properties", k("name", "type"))
j.Set("enum", []string{
"page", "background_page", "service_worker", "shared_worker", "browser", "other",
})
// PageLifecycleEventName
j, _ = json.Gets("domains", k("domain", "Page"), "events", k("name", "lifecycleEvent"), "parameters", k("name", "name"))
j.Set("enum", []string{
"init", "firstPaint", "firstContentfulPaint", "firstImagePaint", "firstMeaningfulPaintCandidate",
"DOMContentLoaded", "load", "networkAlmostIdle", "firstMeaningfulPaint", "networkIdle",
})
// replace these with better type definition
j, _ = getTypes("Input").Gets(k("id", "TimeSinceEpoch"))
j.Set("skip", true)
j, _ = getTypes("Network").Gets(k("id", "TimeSinceEpoch"))
j.Set("skip", true)
j, _ = getTypes("Network").Gets(k("id", "MonotonicTime"))
j.Set("skip", true)
// fix Cookie.Expires
j, _ = getTypes("Network").Gets(k("id", "Cookie"), "properties")
j.Set(index(j.Val(), "name", "expires"), map[string]interface{}{
"$ref": "TimeSinceEpoch",
"description": "Cookie expiration date",
"name": "expires",
})
// deltaX and deltaY are not optional for mouseWheel events
j, _ = json.Gets("domains", k("domain", "Input"), "commands", k("name", "dispatchMouseEvent"), "parameters")
jj, _ := j.Gets(k("name", "deltaX"))
jj.Del("optional")
jj, _ = j.Gets(k("name", "deltaY"))
jj.Del("optional")
// removing the optional for the body as we need to distinguish between no body and empty body
// with that fix we can send an 'empty body' using `SetBody([]byte{})`
// and 'no body' by not calling using 'SetBody()' on the response
j, _ = json.Gets("domains", k("domain", "Fetch"), "commands", k("name", "fulfillRequest"), "parameters")
jj, _ = j.Gets(k("name", "body"))
jj.Del("optional")
}