Skip to content

Latest commit

 

History

History
95 lines (68 loc) · 2.7 KB

emulation.md

File metadata and controls

95 lines (68 loc) · 2.7 KB

Emulation

Rod provides various ways to emulate the environment for pages.

Devices

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.

User agent

If you want to specify a user-agent for a specific page, use Page.SetUserAgent.

Viewport

If you want to specify the viewport for a specific page, use Page.SetViewport.

Locale and timezone

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)

Permissions

Use BrowserGrantPermissions

Geolocation

Use EmulationSetGeolocationOverride

Color scheme and media

Use EmulationSetEmulatedMedia

proto.EmulationSetEmulatedMedia{
    Media: "screen",
    Features: []*proto.EmulationMediaFeature{
        {"prefers-color-scheme", "dark"},
    },
}.Call(page)

Bypass bot detection

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

Next Chapter