Path: blob/main/puppet/test/_CustomBrowserEmulator.ts
1030 views
import {1BrowserEmulatorClassDecorator,2IBrowserEmulator,3IBrowserEmulatorConfig,4} from '@secret-agent/interfaces/ICorePlugin';5import { PluginTypes } from '@secret-agent/interfaces/IPluginTypes';6import IUserAgentOption from '@secret-agent/interfaces/IUserAgentOption';7import DefaultBrowserEmulator from '@secret-agent/default-browser-emulator';8import IViewport from '@secret-agent/interfaces/IViewport';910const id = 'test';11const locale = 'en';12const timezoneId = 'est';13const viewport: IViewport = {14screenHeight: 900,15screenWidth: 1024,16positionY: 0,17positionX: 0,18height: 900,19width: 1024,20};2122const userAgentOption: IUserAgentOption = {23browserName: 'chrome',24browserVersion: {25major: '88',26minor: '0',27},2829operatingSystemPlatform: 'linux',30operatingSystemName: 'linux',31operatingSystemVersion: {32major: '10',33minor: '0',34},3536string: 'Puppet Test',37};3839@BrowserEmulatorClassDecorator40export default class CustomBrowserEmulator implements IBrowserEmulator {41static id = id;42static type: PluginTypes.BrowserEmulator = PluginTypes.BrowserEmulator;4344id = id;45browserName = userAgentOption.browserName;46browserVersion = userAgentOption.browserVersion;47operatingSystemPlatform = userAgentOption.operatingSystemPlatform;48operatingSystemName = userAgentOption.operatingSystemName;49operatingSystemVersion = userAgentOption.operatingSystemVersion;50userAgentString = userAgentOption.string;5152locale = locale;53viewport = viewport;54timezoneId = timezoneId;55deviceProfile = {};5657configure(config: IBrowserEmulatorConfig): void {58config.locale = config.locale || this.locale;59config.viewport = config.viewport || this.viewport;60config.timezoneId = config.timezoneId || this.timezoneId;6162this.locale = config.locale;63this.viewport = config.viewport;64this.timezoneId = config.timezoneId;65}6667async onNewPuppetPage(page) {68const devtools = page.devtoolsSession;69return Promise.all([70devtools.send('Network.setUserAgentOverride', {71userAgent: this.userAgentString,72acceptLanguage: this.locale,73platform: this.operatingSystemPlatform,74}),75devtools76.send('Emulation.setTimezoneOverride', { timezoneId: this.timezoneId })77.catch(() => null),78devtools.send('Emulation.setLocaleOverride', { locale: this.locale }).catch(err => err),79this.viewport80? devtools81.send('Emulation.setDeviceMetricsOverride', {82width: this.viewport.width,83height: this.viewport.height,84deviceScaleFactor: 1,85positionX: this.viewport.positionX,86positionY: this.viewport.positionY,87screenWidth: this.viewport.screenWidth,88screenHeight: this.viewport.screenHeight,89mobile: false,90})91.catch(() => null)92: null,93devtools.send('Emulation.setFocusEmulationEnabled', { enabled: true }).catch(err => err),94]);95}9697static selectBrowserMeta(userAgentSelector?: string) {98return DefaultBrowserEmulator.selectBrowserMeta(userAgentSelector);99}100}101102103