Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
ulixee
GitHub Repository: ulixee/secret-agent
Path: blob/main/puppet/test/_CustomBrowserEmulator.ts
1030 views
1
import {
2
BrowserEmulatorClassDecorator,
3
IBrowserEmulator,
4
IBrowserEmulatorConfig,
5
} from '@secret-agent/interfaces/ICorePlugin';
6
import { PluginTypes } from '@secret-agent/interfaces/IPluginTypes';
7
import IUserAgentOption from '@secret-agent/interfaces/IUserAgentOption';
8
import DefaultBrowserEmulator from '@secret-agent/default-browser-emulator';
9
import IViewport from '@secret-agent/interfaces/IViewport';
10
11
const id = 'test';
12
const locale = 'en';
13
const timezoneId = 'est';
14
const viewport: IViewport = {
15
screenHeight: 900,
16
screenWidth: 1024,
17
positionY: 0,
18
positionX: 0,
19
height: 900,
20
width: 1024,
21
};
22
23
const userAgentOption: IUserAgentOption = {
24
browserName: 'chrome',
25
browserVersion: {
26
major: '88',
27
minor: '0',
28
},
29
30
operatingSystemPlatform: 'linux',
31
operatingSystemName: 'linux',
32
operatingSystemVersion: {
33
major: '10',
34
minor: '0',
35
},
36
37
string: 'Puppet Test',
38
};
39
40
@BrowserEmulatorClassDecorator
41
export default class CustomBrowserEmulator implements IBrowserEmulator {
42
static id = id;
43
static type: PluginTypes.BrowserEmulator = PluginTypes.BrowserEmulator;
44
45
id = id;
46
browserName = userAgentOption.browserName;
47
browserVersion = userAgentOption.browserVersion;
48
operatingSystemPlatform = userAgentOption.operatingSystemPlatform;
49
operatingSystemName = userAgentOption.operatingSystemName;
50
operatingSystemVersion = userAgentOption.operatingSystemVersion;
51
userAgentString = userAgentOption.string;
52
53
locale = locale;
54
viewport = viewport;
55
timezoneId = timezoneId;
56
deviceProfile = {};
57
58
configure(config: IBrowserEmulatorConfig): void {
59
config.locale = config.locale || this.locale;
60
config.viewport = config.viewport || this.viewport;
61
config.timezoneId = config.timezoneId || this.timezoneId;
62
63
this.locale = config.locale;
64
this.viewport = config.viewport;
65
this.timezoneId = config.timezoneId;
66
}
67
68
async onNewPuppetPage(page) {
69
const devtools = page.devtoolsSession;
70
return Promise.all([
71
devtools.send('Network.setUserAgentOverride', {
72
userAgent: this.userAgentString,
73
acceptLanguage: this.locale,
74
platform: this.operatingSystemPlatform,
75
}),
76
devtools
77
.send('Emulation.setTimezoneOverride', { timezoneId: this.timezoneId })
78
.catch(() => null),
79
devtools.send('Emulation.setLocaleOverride', { locale: this.locale }).catch(err => err),
80
this.viewport
81
? devtools
82
.send('Emulation.setDeviceMetricsOverride', {
83
width: this.viewport.width,
84
height: this.viewport.height,
85
deviceScaleFactor: 1,
86
positionX: this.viewport.positionX,
87
positionY: this.viewport.positionY,
88
screenWidth: this.viewport.screenWidth,
89
screenHeight: this.viewport.screenHeight,
90
mobile: false,
91
})
92
.catch(() => null)
93
: null,
94
devtools.send('Emulation.setFocusEmulationEnabled', { enabled: true }).catch(err => err),
95
]);
96
}
97
98
static selectBrowserMeta(userAgentSelector?: string) {
99
return DefaultBrowserEmulator.selectBrowserMeta(userAgentSelector);
100
}
101
}
102
103