forked from chromedp/chromedp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
emulate.go
123 lines (107 loc) · 4.39 KB
/
emulate.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
package chromedp
import (
"github.com/chromedp/cdproto/emulation"
"github.com/fino-digital/chromedp/device"
)
// EmulateAction are actions that change the emulation settings for the
// browser.
type EmulateAction Action
// EmulateViewport is an action to change the browser viewport.
//
// Wraps calls to emulation.SetDeviceMetricsOverride and emulation.SetTouchEmulationEnabled.
//
// Note: this has the effect of setting/forcing the screen orientation to
// landscape, and will disable mobile and touch emulation by default. If this
// is not the desired behavior, use the emulate viewport options
// EmulateOrientation (or EmulateLandscape/EmulatePortrait), EmulateMobile, and
// EmulateTouch, respectively.
func EmulateViewport(width, height int64, opts ...EmulateViewportOption) EmulateAction {
p1 := emulation.SetDeviceMetricsOverride(width, height, 1.0, false)
p2 := emulation.SetTouchEmulationEnabled(false)
for _, o := range opts {
o(p1, p2)
}
return Tasks{p1, p2}
}
// EmulateViewportOption is the type for emulate viewport options.
type EmulateViewportOption = func(*emulation.SetDeviceMetricsOverrideParams, *emulation.SetTouchEmulationEnabledParams)
// EmulateScale is an emulate viewport option to set the device viewport scaling
// factor.
func EmulateScale(scale float64) EmulateViewportOption {
return func(p1 *emulation.SetDeviceMetricsOverrideParams, p2 *emulation.SetTouchEmulationEnabledParams) {
p1.DeviceScaleFactor = scale
}
}
// EmulateOrientation is an emulate viewport option to set the device viewport
// screen orientation.
func EmulateOrientation(orientation emulation.OrientationType, angle int64) EmulateViewportOption {
return func(p1 *emulation.SetDeviceMetricsOverrideParams, p2 *emulation.SetTouchEmulationEnabledParams) {
p1.ScreenOrientation = &emulation.ScreenOrientation{
Type: orientation,
Angle: angle,
}
}
}
// EmulateLandscape is an emulate viewport option to set the device viewport
// screen orientation in landscape primary mode and an angle of 90.
func EmulateLandscape(p1 *emulation.SetDeviceMetricsOverrideParams, p2 *emulation.SetTouchEmulationEnabledParams) {
EmulateOrientation(emulation.OrientationTypeLandscapePrimary, 90)(p1, p2)
}
// EmulatePortrait is an emulate viewport option to set the device viewport
// screen orentation in portrait primary mode and an angle of 0.
func EmulatePortrait(p1 *emulation.SetDeviceMetricsOverrideParams, p2 *emulation.SetTouchEmulationEnabledParams) {
EmulateOrientation(emulation.OrientationTypePortraitPrimary, 0)(p1, p2)
}
// EmulateMobile is an emulate viewport option to toggle the device viewport to
// display as a mobile device.
func EmulateMobile(p1 *emulation.SetDeviceMetricsOverrideParams, p2 *emulation.SetTouchEmulationEnabledParams) {
p1.Mobile = true
}
// EmulateTouch is an emulate viewport option to enable touch emulation.
func EmulateTouch(p1 *emulation.SetDeviceMetricsOverrideParams, p2 *emulation.SetTouchEmulationEnabledParams) {
p2.Enabled = true
}
// ResetViewport is an action to reset the browser viewport to the default
// values the browser was started with.
//
// Note: does not modify / change the browser's emulated User-Agent, if any.
func ResetViewport() EmulateAction {
return EmulateViewport(0, 0, EmulatePortrait)
}
// Device is the shared interface for known device types.
//
// See: github.com/chromedp/chromedp/device for a set of off-the-shelf devices
// and modes.
type Device interface {
// Device returns the device info.
Device() device.Info
}
// Emulate is an action to emulate a specific device.
//
// See: github.com/chromedp/chromedp/device for a set of off-the-shelf devices
// and modes.
func Emulate(device Device) EmulateAction {
d := device.Device()
var angle int64
orientation := emulation.OrientationTypePortraitPrimary
if d.Landscape {
orientation, angle = emulation.OrientationTypeLandscapePrimary, 90
}
return Tasks{
emulation.SetUserAgentOverride(d.UserAgent),
emulation.SetDeviceMetricsOverride(d.Width, d.Height, d.Scale, d.Mobile).
WithScreenOrientation(&emulation.ScreenOrientation{
Type: orientation,
Angle: angle,
}),
emulation.SetTouchEmulationEnabled(d.Touch),
}
}
// EmulateReset is an action to reset the device emulation.
//
// Resets the browser's viewport, screen orientation, user-agent, and
// mobile/touch emulation settings to the original values the browser was
// started with.
func EmulateReset() EmulateAction {
return Emulate(device.Reset)
}