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