-
-
Notifications
You must be signed in to change notification settings - Fork 320
/
utils.go
95 lines (83 loc) · 2 KB
/
utils.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
package devices
import (
"github.com/go-rod/rod/lib/proto"
"github.com/ysmood/gson"
)
// Device for devices
type Device struct {
gson.JSON
}
// Clear is used to clear overrides
var Clear = Device{}
// Test device
var Test = Device{gson.NewFrom(`{
"screen": {
"device-pixel-ratio": 1,
"horizontal": {
"height": 600,
"width": 800
},
"vertical": {
"height": 600,
"width": 800
}
},
"user-agent": "Test Agent"
}`)}
// Metrics config
func (device Device) Metrics(landscape bool) *proto.EmulationSetDeviceMetricsOverride {
if device == Clear {
return nil
}
var screen gson.JSON
var orientation *proto.EmulationScreenOrientation
if landscape {
screen = device.Get("screen.horizontal")
orientation = &proto.EmulationScreenOrientation{
Angle: 90,
Type: proto.EmulationScreenOrientationTypeLandscapePrimary,
}
} else {
screen = device.Get("screen.vertical")
orientation = &proto.EmulationScreenOrientation{
Angle: 0,
Type: proto.EmulationScreenOrientationTypePortraitPrimary,
}
}
return &proto.EmulationSetDeviceMetricsOverride{
Width: screen.Get("width").Int(),
Height: screen.Get("height").Int(),
DeviceScaleFactor: device.Get("screen.device-pixel-ratio").Num(),
ScreenOrientation: orientation,
Mobile: has(device.Get("capabilities"), "mobile"),
}
}
// Touch config
func (device Device) Touch() *proto.EmulationSetTouchEmulationEnabled {
if device == Clear {
return &proto.EmulationSetTouchEmulationEnabled{
Enabled: false,
}
}
return &proto.EmulationSetTouchEmulationEnabled{
Enabled: has(device.Get("capabilities"), "touch"),
MaxTouchPoints: 5,
}
}
// UserAgent config
func (device Device) UserAgent() *proto.NetworkSetUserAgentOverride {
if device == Clear {
return nil
}
return &proto.NetworkSetUserAgentOverride{
UserAgent: device.Get("user-agent").String(),
}
}
func has(arr gson.JSON, str string) bool {
for _, item := range arr.Arr() {
if item.Str() == str {
return true
}
}
return false
}