2017-07-18 01:13:04 +00:00
|
|
|
/**
|
|
|
|
* Copyright 2017 Google Inc. All rights reserved.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
2023-09-15 11:00:20 +00:00
|
|
|
import {type Protocol} from 'devtools-protocol';
|
2023-02-15 23:09:31 +00:00
|
|
|
|
2023-09-15 11:00:20 +00:00
|
|
|
import {type CDPSession, CDPSessionEvent} from '../api/CDPSession.js';
|
|
|
|
import {type GeolocationOptions, type MediaFeature} from '../api/Page.js';
|
2023-09-16 20:56:40 +00:00
|
|
|
import {debugError} from '../common/util.js';
|
|
|
|
import {type Viewport} from '../common/Viewport.js';
|
2023-06-15 12:30:37 +00:00
|
|
|
import {assert} from '../util/assert.js';
|
2023-09-07 08:05:57 +00:00
|
|
|
import {invokeAtMostOnceForArguments} from '../util/decorators.js';
|
2023-06-15 12:30:37 +00:00
|
|
|
import {isErrorLike} from '../util/ErrorLike.js';
|
|
|
|
|
2023-09-07 08:05:57 +00:00
|
|
|
interface ViewportState {
|
|
|
|
viewport?: Viewport;
|
2023-09-13 10:15:22 +00:00
|
|
|
active: boolean;
|
2023-09-07 08:05:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
interface IdleOverridesState {
|
|
|
|
overrides?: {
|
|
|
|
isUserActive: boolean;
|
|
|
|
isScreenUnlocked: boolean;
|
|
|
|
};
|
|
|
|
active: boolean;
|
|
|
|
}
|
2017-07-18 01:13:04 +00:00
|
|
|
|
2023-09-07 09:57:19 +00:00
|
|
|
interface TimezoneState {
|
|
|
|
timezoneId?: string;
|
|
|
|
active: boolean;
|
|
|
|
}
|
|
|
|
|
2023-09-11 15:39:35 +00:00
|
|
|
interface VisionDeficiencyState {
|
|
|
|
visionDeficiency?: Protocol.Emulation.SetEmulatedVisionDeficiencyRequest['type'];
|
|
|
|
active: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface CpuThrottlingState {
|
|
|
|
factor?: number;
|
|
|
|
active: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface MediaFeaturesState {
|
|
|
|
mediaFeatures?: MediaFeature[];
|
|
|
|
active: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface MediaTypeState {
|
|
|
|
type?: string;
|
|
|
|
active: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface GeoLocationState {
|
|
|
|
geoLocation?: GeolocationOptions;
|
|
|
|
active: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface DefaultBackgroundColorState {
|
|
|
|
color?: Protocol.DOM.RGBA;
|
|
|
|
active: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface JavascriptEnabledState {
|
|
|
|
javaScriptEnabled: boolean;
|
|
|
|
active: boolean;
|
|
|
|
}
|
|
|
|
|
2023-09-13 10:15:22 +00:00
|
|
|
interface ClientProvider {
|
|
|
|
clients(): CDPSession[];
|
2023-09-18 17:38:39 +00:00
|
|
|
registerState(state: EmulatedState<any>): void;
|
2023-09-13 10:15:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class EmulatedState<T extends {active: boolean}> {
|
|
|
|
#state: T;
|
|
|
|
#clientProvider: ClientProvider;
|
|
|
|
#updater: (client: CDPSession, state: T) => Promise<void>;
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
initialState: T,
|
|
|
|
clientProvider: ClientProvider,
|
|
|
|
updater: (client: CDPSession, state: T) => Promise<void>
|
|
|
|
) {
|
|
|
|
this.#state = initialState;
|
|
|
|
this.#clientProvider = clientProvider;
|
|
|
|
this.#updater = updater;
|
2023-09-18 17:38:39 +00:00
|
|
|
this.#clientProvider.registerState(this);
|
2023-09-13 10:15:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async setState(state: T): Promise<void> {
|
|
|
|
this.#state = state;
|
|
|
|
await this.sync();
|
|
|
|
}
|
|
|
|
|
2023-09-18 17:38:39 +00:00
|
|
|
get state(): T {
|
|
|
|
return this.#state;
|
|
|
|
}
|
|
|
|
|
2023-09-13 10:15:22 +00:00
|
|
|
async sync(): Promise<void> {
|
|
|
|
await Promise.all(
|
|
|
|
this.#clientProvider.clients().map(client => {
|
|
|
|
return this.#updater(client, this.#state);
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-27 07:24:23 +00:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
2020-04-28 16:45:34 +00:00
|
|
|
export class EmulationManager {
|
2022-06-13 09:16:25 +00:00
|
|
|
#client: CDPSession;
|
2023-09-11 15:39:35 +00:00
|
|
|
|
2022-06-13 09:16:25 +00:00
|
|
|
#emulatingMobile = false;
|
|
|
|
#hasTouch = false;
|
2020-04-21 08:20:25 +00:00
|
|
|
|
2023-09-18 17:38:39 +00:00
|
|
|
#states: Array<EmulatedState<any>> = [];
|
|
|
|
|
2023-09-13 10:15:22 +00:00
|
|
|
#viewportState = new EmulatedState<ViewportState>(
|
|
|
|
{
|
|
|
|
active: false,
|
|
|
|
},
|
|
|
|
this,
|
|
|
|
this.#applyViewport
|
|
|
|
);
|
2023-09-18 17:38:39 +00:00
|
|
|
#idleOverridesState = new EmulatedState<IdleOverridesState>(
|
|
|
|
{
|
|
|
|
active: false,
|
|
|
|
},
|
|
|
|
this,
|
|
|
|
this.#emulateIdleState
|
|
|
|
);
|
|
|
|
#timezoneState = new EmulatedState<TimezoneState>(
|
|
|
|
{
|
|
|
|
active: false,
|
|
|
|
},
|
|
|
|
this,
|
|
|
|
this.#emulateTimezone
|
|
|
|
);
|
|
|
|
#visionDeficiencyState = new EmulatedState<VisionDeficiencyState>(
|
|
|
|
{
|
|
|
|
active: false,
|
|
|
|
},
|
|
|
|
this,
|
|
|
|
this.#emulateVisionDeficiency
|
|
|
|
);
|
|
|
|
#cpuThrottlingState = new EmulatedState<CpuThrottlingState>(
|
|
|
|
{
|
|
|
|
active: false,
|
|
|
|
},
|
|
|
|
this,
|
|
|
|
this.#emulateCpuThrottling
|
|
|
|
);
|
|
|
|
#mediaFeaturesState = new EmulatedState<MediaFeaturesState>(
|
|
|
|
{
|
|
|
|
active: false,
|
|
|
|
},
|
|
|
|
this,
|
|
|
|
this.#emulateMediaFeatures
|
|
|
|
);
|
|
|
|
#mediaTypeState = new EmulatedState<MediaTypeState>(
|
|
|
|
{
|
|
|
|
active: false,
|
|
|
|
},
|
|
|
|
this,
|
|
|
|
this.#emulateMediaType
|
|
|
|
);
|
|
|
|
#geoLocationState = new EmulatedState<GeoLocationState>(
|
|
|
|
{
|
|
|
|
active: false,
|
|
|
|
},
|
|
|
|
this,
|
|
|
|
this.#setGeolocation
|
|
|
|
);
|
|
|
|
#defaultBackgroundColorState = new EmulatedState<DefaultBackgroundColorState>(
|
|
|
|
{
|
|
|
|
active: false,
|
|
|
|
},
|
|
|
|
this,
|
|
|
|
this.#setDefaultBackgroundColor
|
|
|
|
);
|
|
|
|
#javascriptEnabledState = new EmulatedState<JavascriptEnabledState>(
|
|
|
|
{
|
|
|
|
javaScriptEnabled: true,
|
|
|
|
active: false,
|
|
|
|
},
|
|
|
|
this,
|
|
|
|
this.#setJavaScriptEnabled
|
|
|
|
);
|
2023-09-11 15:39:35 +00:00
|
|
|
|
2023-08-30 09:04:28 +00:00
|
|
|
#secondaryClients = new Set<CDPSession>();
|
|
|
|
|
2020-04-28 16:45:34 +00:00
|
|
|
constructor(client: CDPSession) {
|
2022-06-13 09:16:25 +00:00
|
|
|
this.#client = client;
|
2017-07-30 02:12:17 +00:00
|
|
|
}
|
|
|
|
|
2023-08-28 06:20:57 +00:00
|
|
|
updateClient(client: CDPSession): void {
|
|
|
|
this.#client = client;
|
2023-08-30 09:04:28 +00:00
|
|
|
this.#secondaryClients.delete(client);
|
|
|
|
}
|
|
|
|
|
2023-09-18 17:38:39 +00:00
|
|
|
registerState(state: EmulatedState<any>): void {
|
|
|
|
this.#states.push(state);
|
|
|
|
}
|
|
|
|
|
|
|
|
clients(): CDPSession[] {
|
|
|
|
return [this.#client, ...Array.from(this.#secondaryClients)];
|
|
|
|
}
|
|
|
|
|
2023-08-30 13:07:11 +00:00
|
|
|
async registerSpeculativeSession(client: CDPSession): Promise<void> {
|
2023-08-30 09:04:28 +00:00
|
|
|
this.#secondaryClients.add(client);
|
2023-09-13 13:47:55 +00:00
|
|
|
client.once(CDPSessionEvent.Disconnected, () => {
|
|
|
|
this.#secondaryClients.delete(client);
|
2023-08-30 09:04:28 +00:00
|
|
|
});
|
2023-09-07 09:57:19 +00:00
|
|
|
// We don't await here because we want to register all state changes before
|
|
|
|
// the target is unpaused.
|
2023-09-18 17:38:39 +00:00
|
|
|
void Promise.all(
|
|
|
|
this.#states.map(s => {
|
|
|
|
return s.sync().catch(debugError);
|
|
|
|
})
|
|
|
|
);
|
2023-08-28 06:20:57 +00:00
|
|
|
}
|
|
|
|
|
2023-06-15 12:30:37 +00:00
|
|
|
get javascriptEnabled(): boolean {
|
2023-09-18 17:38:39 +00:00
|
|
|
return this.#javascriptEnabledState.state.javaScriptEnabled;
|
2023-09-13 10:15:22 +00:00
|
|
|
}
|
|
|
|
|
2020-05-06 13:23:07 +00:00
|
|
|
async emulateViewport(viewport: Viewport): Promise<boolean> {
|
2023-09-13 10:15:22 +00:00
|
|
|
await this.#viewportState.setState({
|
2023-09-07 08:05:57 +00:00
|
|
|
viewport,
|
2023-09-13 10:15:22 +00:00
|
|
|
active: true,
|
|
|
|
});
|
2023-08-30 09:04:28 +00:00
|
|
|
|
|
|
|
const mobile = viewport.isMobile || false;
|
|
|
|
const hasTouch = viewport.hasTouch || false;
|
|
|
|
const reloadNeeded =
|
|
|
|
this.#emulatingMobile !== mobile || this.#hasTouch !== hasTouch;
|
|
|
|
this.#emulatingMobile = mobile;
|
|
|
|
this.#hasTouch = hasTouch;
|
|
|
|
|
|
|
|
return reloadNeeded;
|
|
|
|
}
|
|
|
|
|
2023-09-07 08:05:57 +00:00
|
|
|
@invokeAtMostOnceForArguments
|
|
|
|
async #applyViewport(
|
|
|
|
client: CDPSession,
|
|
|
|
viewportState: ViewportState
|
|
|
|
): Promise<void> {
|
|
|
|
if (!viewportState.viewport) {
|
2023-08-30 09:04:28 +00:00
|
|
|
return;
|
|
|
|
}
|
2023-09-07 08:05:57 +00:00
|
|
|
const {viewport} = viewportState;
|
2017-07-18 01:13:04 +00:00
|
|
|
const mobile = viewport.isMobile || false;
|
|
|
|
const width = viewport.width;
|
|
|
|
const height = viewport.height;
|
2023-03-24 09:56:52 +00:00
|
|
|
const deviceScaleFactor = viewport.deviceScaleFactor ?? 1;
|
2021-05-12 14:48:30 +00:00
|
|
|
const screenOrientation: Protocol.Emulation.ScreenOrientation =
|
|
|
|
viewport.isLandscape
|
2022-06-22 13:25:44 +00:00
|
|
|
? {angle: 90, type: 'landscapePrimary'}
|
|
|
|
: {angle: 0, type: 'portraitPrimary'};
|
2018-06-29 01:48:44 +00:00
|
|
|
const hasTouch = viewport.hasTouch || false;
|
2017-07-18 01:13:04 +00:00
|
|
|
|
|
|
|
await Promise.all([
|
2023-08-30 09:04:28 +00:00
|
|
|
client.send('Emulation.setDeviceMetricsOverride', {
|
2020-05-07 10:54:55 +00:00
|
|
|
mobile,
|
|
|
|
width,
|
|
|
|
height,
|
|
|
|
deviceScaleFactor,
|
|
|
|
screenOrientation,
|
|
|
|
}),
|
2023-08-30 09:04:28 +00:00
|
|
|
client.send('Emulation.setTouchEmulationEnabled', {
|
2020-05-07 10:54:55 +00:00
|
|
|
enabled: hasTouch,
|
|
|
|
}),
|
2017-07-18 01:13:04 +00:00
|
|
|
]);
|
|
|
|
}
|
2023-06-15 12:30:37 +00:00
|
|
|
|
|
|
|
async emulateIdleState(overrides?: {
|
|
|
|
isUserActive: boolean;
|
|
|
|
isScreenUnlocked: boolean;
|
|
|
|
}): Promise<void> {
|
2023-09-18 17:38:39 +00:00
|
|
|
await this.#idleOverridesState.setState({
|
2023-09-07 08:05:57 +00:00
|
|
|
active: true,
|
|
|
|
overrides,
|
2023-09-18 17:38:39 +00:00
|
|
|
});
|
2023-09-07 08:05:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@invokeAtMostOnceForArguments
|
|
|
|
async #emulateIdleState(
|
|
|
|
client: CDPSession,
|
|
|
|
idleStateState: IdleOverridesState
|
|
|
|
): Promise<void> {
|
|
|
|
if (!idleStateState.active) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (idleStateState.overrides) {
|
|
|
|
await client.send('Emulation.setIdleOverride', {
|
|
|
|
isUserActive: idleStateState.overrides.isUserActive,
|
|
|
|
isScreenUnlocked: idleStateState.overrides.isScreenUnlocked,
|
2023-06-15 12:30:37 +00:00
|
|
|
});
|
|
|
|
} else {
|
2023-09-07 08:05:57 +00:00
|
|
|
await client.send('Emulation.clearIdleOverride');
|
2023-06-15 12:30:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-11 15:39:35 +00:00
|
|
|
@invokeAtMostOnceForArguments
|
2023-09-07 09:57:19 +00:00
|
|
|
async #emulateTimezone(
|
|
|
|
client: CDPSession,
|
|
|
|
timezoneState: TimezoneState
|
|
|
|
): Promise<void> {
|
|
|
|
if (!timezoneState.active) {
|
|
|
|
return;
|
|
|
|
}
|
2023-06-15 12:30:37 +00:00
|
|
|
try {
|
2023-09-07 09:57:19 +00:00
|
|
|
await client.send('Emulation.setTimezoneOverride', {
|
|
|
|
timezoneId: timezoneState.timezoneId || '',
|
2023-06-15 12:30:37 +00:00
|
|
|
});
|
|
|
|
} catch (error) {
|
|
|
|
if (isErrorLike(error) && error.message.includes('Invalid timezone')) {
|
2023-09-07 09:57:19 +00:00
|
|
|
throw new Error(`Invalid timezone ID: ${timezoneState.timezoneId}`);
|
2023-06-15 12:30:37 +00:00
|
|
|
}
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-07 09:57:19 +00:00
|
|
|
async emulateTimezone(timezoneId?: string): Promise<void> {
|
2023-09-18 17:38:39 +00:00
|
|
|
await this.#timezoneState.setState({
|
2023-09-07 09:57:19 +00:00
|
|
|
timezoneId,
|
|
|
|
active: true,
|
2023-09-18 17:38:39 +00:00
|
|
|
});
|
2023-09-07 09:57:19 +00:00
|
|
|
}
|
|
|
|
|
2023-09-11 15:39:35 +00:00
|
|
|
@invokeAtMostOnceForArguments
|
|
|
|
async #emulateVisionDeficiency(
|
|
|
|
client: CDPSession,
|
|
|
|
visionDeficiency: VisionDeficiencyState
|
|
|
|
): Promise<void> {
|
|
|
|
if (!visionDeficiency.active) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
await client.send('Emulation.setEmulatedVisionDeficiency', {
|
|
|
|
type: visionDeficiency.visionDeficiency || 'none',
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-06-15 12:30:37 +00:00
|
|
|
async emulateVisionDeficiency(
|
|
|
|
type?: Protocol.Emulation.SetEmulatedVisionDeficiencyRequest['type']
|
|
|
|
): Promise<void> {
|
|
|
|
const visionDeficiencies = new Set<
|
|
|
|
Protocol.Emulation.SetEmulatedVisionDeficiencyRequest['type']
|
|
|
|
>([
|
|
|
|
'none',
|
|
|
|
'achromatopsia',
|
|
|
|
'blurredVision',
|
|
|
|
'deuteranopia',
|
|
|
|
'protanopia',
|
|
|
|
'tritanopia',
|
|
|
|
]);
|
2023-09-11 15:39:35 +00:00
|
|
|
assert(
|
|
|
|
!type || visionDeficiencies.has(type),
|
|
|
|
`Unsupported vision deficiency: ${type}`
|
|
|
|
);
|
2023-09-18 17:38:39 +00:00
|
|
|
await this.#visionDeficiencyState.setState({
|
2023-09-11 15:39:35 +00:00
|
|
|
active: true,
|
|
|
|
visionDeficiency: type,
|
2023-09-18 17:38:39 +00:00
|
|
|
});
|
2023-09-11 15:39:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@invokeAtMostOnceForArguments
|
|
|
|
async #emulateCpuThrottling(
|
|
|
|
client: CDPSession,
|
|
|
|
state: CpuThrottlingState
|
|
|
|
): Promise<void> {
|
|
|
|
if (!state.active) {
|
|
|
|
return;
|
2023-06-15 12:30:37 +00:00
|
|
|
}
|
2023-09-11 15:39:35 +00:00
|
|
|
await client.send('Emulation.setCPUThrottlingRate', {
|
|
|
|
rate: state.factor ?? 1,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-06-15 12:30:37 +00:00
|
|
|
async emulateCPUThrottling(factor: number | null): Promise<void> {
|
|
|
|
assert(
|
|
|
|
factor === null || factor >= 1,
|
|
|
|
'Throttling rate should be greater or equal to 1'
|
|
|
|
);
|
2023-09-18 17:38:39 +00:00
|
|
|
await this.#cpuThrottlingState.setState({
|
2023-09-11 15:39:35 +00:00
|
|
|
active: true,
|
|
|
|
factor: factor ?? undefined,
|
2023-09-18 17:38:39 +00:00
|
|
|
});
|
2023-09-11 15:39:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@invokeAtMostOnceForArguments
|
|
|
|
async #emulateMediaFeatures(
|
|
|
|
client: CDPSession,
|
|
|
|
state: MediaFeaturesState
|
|
|
|
): Promise<void> {
|
|
|
|
if (!state.active) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
await client.send('Emulation.setEmulatedMedia', {
|
|
|
|
features: state.mediaFeatures,
|
2023-06-15 12:30:37 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async emulateMediaFeatures(features?: MediaFeature[]): Promise<void> {
|
|
|
|
if (Array.isArray(features)) {
|
|
|
|
for (const mediaFeature of features) {
|
|
|
|
const name = mediaFeature.name;
|
|
|
|
assert(
|
|
|
|
/^(?:prefers-(?:color-scheme|reduced-motion)|color-gamut)$/.test(
|
|
|
|
name
|
|
|
|
),
|
|
|
|
'Unsupported media feature: ' + name
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2023-09-18 17:38:39 +00:00
|
|
|
await this.#mediaFeaturesState.setState({
|
2023-09-11 15:39:35 +00:00
|
|
|
active: true,
|
|
|
|
mediaFeatures: features,
|
2023-09-18 17:38:39 +00:00
|
|
|
});
|
2023-09-11 15:39:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@invokeAtMostOnceForArguments
|
|
|
|
async #emulateMediaType(
|
|
|
|
client: CDPSession,
|
|
|
|
state: MediaTypeState
|
|
|
|
): Promise<void> {
|
|
|
|
if (!state.active) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
await client.send('Emulation.setEmulatedMedia', {
|
|
|
|
media: state.type || '',
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-06-15 12:30:37 +00:00
|
|
|
async emulateMediaType(type?: string): Promise<void> {
|
|
|
|
assert(
|
|
|
|
type === 'screen' ||
|
|
|
|
type === 'print' ||
|
|
|
|
(type ?? undefined) === undefined,
|
|
|
|
'Unsupported media type: ' + type
|
|
|
|
);
|
2023-09-18 17:38:39 +00:00
|
|
|
await this.#mediaTypeState.setState({
|
2023-09-11 15:39:35 +00:00
|
|
|
type,
|
|
|
|
active: true,
|
2023-09-18 17:38:39 +00:00
|
|
|
});
|
2023-09-11 15:39:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@invokeAtMostOnceForArguments
|
|
|
|
async #setGeolocation(
|
|
|
|
client: CDPSession,
|
|
|
|
state: GeoLocationState
|
|
|
|
): Promise<void> {
|
|
|
|
if (!state.active) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
await client.send(
|
|
|
|
'Emulation.setGeolocationOverride',
|
|
|
|
state.geoLocation
|
|
|
|
? {
|
|
|
|
longitude: state.geoLocation.longitude,
|
|
|
|
latitude: state.geoLocation.latitude,
|
|
|
|
accuracy: state.geoLocation.accuracy,
|
|
|
|
}
|
|
|
|
: undefined
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-06-15 12:30:37 +00:00
|
|
|
async setGeolocation(options: GeolocationOptions): Promise<void> {
|
|
|
|
const {longitude, latitude, accuracy = 0} = options;
|
|
|
|
if (longitude < -180 || longitude > 180) {
|
|
|
|
throw new Error(
|
|
|
|
`Invalid longitude "${longitude}": precondition -180 <= LONGITUDE <= 180 failed.`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if (latitude < -90 || latitude > 90) {
|
|
|
|
throw new Error(
|
|
|
|
`Invalid latitude "${latitude}": precondition -90 <= LATITUDE <= 90 failed.`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if (accuracy < 0) {
|
|
|
|
throw new Error(
|
|
|
|
`Invalid accuracy "${accuracy}": precondition 0 <= ACCURACY failed.`
|
|
|
|
);
|
|
|
|
}
|
2023-09-18 17:38:39 +00:00
|
|
|
await this.#geoLocationState.setState({
|
2023-09-11 15:39:35 +00:00
|
|
|
active: true,
|
|
|
|
geoLocation: {
|
|
|
|
longitude,
|
|
|
|
latitude,
|
|
|
|
accuracy,
|
|
|
|
},
|
2023-09-18 17:38:39 +00:00
|
|
|
});
|
2023-09-11 15:39:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@invokeAtMostOnceForArguments
|
|
|
|
async #setDefaultBackgroundColor(
|
|
|
|
client: CDPSession,
|
|
|
|
state: DefaultBackgroundColorState
|
|
|
|
): Promise<void> {
|
|
|
|
if (!state.active) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
await client.send('Emulation.setDefaultBackgroundColorOverride', {
|
|
|
|
color: state.color,
|
2023-06-15 12:30:37 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Resets default white background
|
|
|
|
*/
|
|
|
|
async resetDefaultBackgroundColor(): Promise<void> {
|
2023-09-18 17:38:39 +00:00
|
|
|
await this.#defaultBackgroundColorState.setState({
|
2023-09-11 15:39:35 +00:00
|
|
|
active: true,
|
|
|
|
color: undefined,
|
2023-09-18 17:38:39 +00:00
|
|
|
});
|
2023-06-15 12:30:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Hides default white background
|
|
|
|
*/
|
|
|
|
async setTransparentBackgroundColor(): Promise<void> {
|
2023-09-18 17:38:39 +00:00
|
|
|
await this.#defaultBackgroundColorState.setState({
|
2023-09-11 15:39:35 +00:00
|
|
|
active: true,
|
2023-06-15 12:30:37 +00:00
|
|
|
color: {r: 0, g: 0, b: 0, a: 0},
|
2023-09-18 17:38:39 +00:00
|
|
|
});
|
2023-06-15 12:30:37 +00:00
|
|
|
}
|
|
|
|
|
2023-09-11 15:39:35 +00:00
|
|
|
@invokeAtMostOnceForArguments
|
|
|
|
async #setJavaScriptEnabled(
|
|
|
|
client: CDPSession,
|
|
|
|
state: JavascriptEnabledState
|
|
|
|
): Promise<void> {
|
|
|
|
if (!state.active) {
|
2023-06-15 12:30:37 +00:00
|
|
|
return;
|
|
|
|
}
|
2023-09-11 15:39:35 +00:00
|
|
|
await client.send('Emulation.setScriptExecutionDisabled', {
|
|
|
|
value: !state.javaScriptEnabled,
|
2023-06-15 12:30:37 +00:00
|
|
|
});
|
|
|
|
}
|
2023-09-11 15:39:35 +00:00
|
|
|
|
|
|
|
async setJavaScriptEnabled(enabled: boolean): Promise<void> {
|
2023-09-18 17:38:39 +00:00
|
|
|
await this.#javascriptEnabledState.setState({
|
2023-09-11 15:39:35 +00:00
|
|
|
active: true,
|
|
|
|
javaScriptEnabled: enabled,
|
2023-09-18 17:38:39 +00:00
|
|
|
});
|
2023-09-11 15:39:35 +00:00
|
|
|
}
|
2017-07-18 01:13:04 +00:00
|
|
|
}
|