forked from go-rod/rod
-
Notifications
You must be signed in to change notification settings - Fork 0
/
device.go
101 lines (87 loc) · 2.24 KB
/
device.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
// Package devices ...
package devices
import (
"github.com/go-rod/rod/lib/proto"
"github.com/ysmood/gson"
)
// Device represents a emulated device.
type Device struct {
Capabilities []string
UserAgent string
AcceptLanguage string
Screen Screen
Title string
landscape bool
clear bool
}
// Screen represents the screen of a device.
type Screen struct {
DevicePixelRatio float64
Horizontal ScreenSize
Vertical ScreenSize
}
// ScreenSize represents the size of the screen.
type ScreenSize struct {
Width int
Height int
}
// Landscape clones the device and set it to landscape mode
func (device Device) Landscape() Device {
d := device
d.landscape = true
return d
}
// MetricsEmulation config
func (device Device) MetricsEmulation() *proto.EmulationSetDeviceMetricsOverride {
if device.IsClear() {
return nil
}
var screen ScreenSize
var orientation *proto.EmulationScreenOrientation
if device.landscape {
screen = device.Screen.Horizontal
orientation = &proto.EmulationScreenOrientation{
Angle: 90,
Type: proto.EmulationScreenOrientationTypeLandscapePrimary,
}
} else {
screen = device.Screen.Vertical
orientation = &proto.EmulationScreenOrientation{
Angle: 0,
Type: proto.EmulationScreenOrientationTypePortraitPrimary,
}
}
return &proto.EmulationSetDeviceMetricsOverride{
Width: screen.Width,
Height: screen.Height,
DeviceScaleFactor: device.Screen.DevicePixelRatio,
ScreenOrientation: orientation,
Mobile: has(device.Capabilities, "mobile"),
}
}
// TouchEmulation config
func (device Device) TouchEmulation() *proto.EmulationSetTouchEmulationEnabled {
if device.IsClear() {
return &proto.EmulationSetTouchEmulationEnabled{
Enabled: false,
}
}
return &proto.EmulationSetTouchEmulationEnabled{
Enabled: has(device.Capabilities, "touch"),
MaxTouchPoints: gson.Int(5),
}
}
// UserAgentEmulation config
func (device Device) UserAgentEmulation() *proto.NetworkSetUserAgentOverride {
if device.IsClear() {
return nil
}
return &proto.NetworkSetUserAgentOverride{
UserAgent: device.UserAgent,
AcceptLanguage: device.AcceptLanguage,
}
}
// IsClear type
func (device Device) IsClear() bool {
return device.clear
}