Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
ulixee
GitHub Repository: ulixee/secret-agent
Path: blob/main/plugins/default-browser-emulator/lib/helpers/setGeolocation.ts
1030 views
1
import { assert } from '@secret-agent/commons/utils';
2
import { IPuppetPage } from '@secret-agent/interfaces/IPuppetPage';
3
import BrowserEmulator from '../../index';
4
5
export default function setActiveAndFocused(
6
emulator: BrowserEmulator,
7
page: IPuppetPage,
8
): Promise<any> {
9
const location = emulator.geolocation;
10
if (!location) return;
11
12
assert(Math.abs(location.latitude) <= 90, 'Latitude must be in a range from -90 to 90');
13
assert(Math.abs(location.longitude) <= 180, 'Longitude must be in a range from -180 to 180');
14
15
if (!location.accuracy) location.accuracy = 50 - Math.floor(Math.random() * 10);
16
assert(location.accuracy >= 0, 'Accuracy must be a number greater than or equal to 0');
17
18
return Promise.all([
19
page.devtoolsSession.send('Emulation.setGeolocationOverride', {
20
...location,
21
}),
22
page.browserContext.sendWithBrowserDevtoolsSession('Browser.grantPermissions', {
23
permissions: ['geolocation'],
24
browserContextId: page.browserContext.id,
25
}),
26
]);
27
}
28
29