Rod provides various ways to emulate the environment for pages.
To set the viewport, user-agent, orientation, etc at the same time for a page, you can use the predefined devices:
page.MustEmulate(devices.IPhone6or7or8Plus)
Or define your own device:
page.MustEmulate(devices.New(`{
"capabilities": [
"touch",
"mobile"
],
"screen": {
"device-pixel-ratio": 2,
"horizontal": {
"height": 320,
"width": 480
},
"vertical": {
"height": 480,
"width": 320
}
},
"show-by-default": false,
"title": "iPhone 4",
"type": "phone",
"user-agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 7_1_2 like Mac OS X)"
}`))
Check the source code of the predefined devices, the fields should self explain themselves.
You can also set the default device for all pages by using Browser.DefaultDevice.
If you want to specify a user-agent for a specific page, use Page.SetUserAgent.
If you want to specify the viewport for a specific page, use Page.SetViewport.
You can use the launch env to set for all pages:
u := launcher.New().Env("TZ=America/New_York").MustConnect()
browser := rod.New().ControlURL(u).MustConnect()
Or you can use EmulationSetTimezoneOverride or EmulationSetLocaleOverride to set for a specific page:
proto.EmulationSetTimezoneOverride{TimezoneID: "America/New_York"}.Call(page)
Use EmulationSetGeolocationOverride
proto.EmulationSetEmulatedMedia{
Media: "screen",
Features: []*proto.EmulationMediaFeature{
{"prefers-color-scheme", "dark"},
},
}.Call(page)
When we control a page we hope it's completely transparent for the page so that the page cannot tell if it's under control or not. Sure you can handcraft one, but here's one tested solution that might help: code example