From d6fff31a4f097da9d18d766df22630c9e1805112 Mon Sep 17 00:00:00 2001 From: z0ccc Date: Thu, 29 Dec 2022 19:12:12 -0500 Subject: [PATCH] Restructured location page --- src/Popup/Components/DebouncedInput.tsx | 6 +- src/Popup/Pages/LocationPage/index.tsx | 215 ++++++++++-------------- src/Popup/Pages/UserAgentPage/index.tsx | 6 +- src/utils/attachDebugger.ts | 87 +++++----- 4 files changed, 141 insertions(+), 173 deletions(-) diff --git a/src/Popup/Components/DebouncedInput.tsx b/src/Popup/Components/DebouncedInput.tsx index d18e26f..c96dac0 100644 --- a/src/Popup/Components/DebouncedInput.tsx +++ b/src/Popup/Components/DebouncedInput.tsx @@ -1,5 +1,5 @@ import { Dispatch, SetStateAction, ChangeEvent, useMemo } from 'react' -import { Label, Input } from 'theme-ui' +import { Label, Input, Box } from 'theme-ui' import detachDebugger from '../../utils/detachDebugger' import debounce from 'lodash.debounce' @@ -36,10 +36,10 @@ const DebouncedInput = ({ } return ( - <> + - + ) } diff --git a/src/Popup/Pages/LocationPage/index.tsx b/src/Popup/Pages/LocationPage/index.tsx index e359f00..ee5868f 100644 --- a/src/Popup/Pages/LocationPage/index.tsx +++ b/src/Popup/Pages/LocationPage/index.tsx @@ -1,6 +1,7 @@ import { useState, useEffect, ChangeEvent, useCallback } from 'react' import { Box, Button, Flex, Label, Radio, Select } from 'theme-ui' import Page from '../../Components/Page' +import Checkbox from '../../Components/CheckBox' import DebouncedInput from '../../Components/DebouncedInput' import detachDebugger from '../../../utils/detachDebugger' import countryLocales from '../../../utils/countryLocales' @@ -15,9 +16,9 @@ interface LocationPageProps { } const LocationPage = ({ tab, setTab }: LocationPageProps) => { + const [browserDefault, setBrowserDefault] = useState(true) const [ipData, setIpData] = useState() const [ipInfo, setIpInfo] = useState('loading...') - const [locationType, setLocationType] = useState('') const [timezone, setTimezone] = useState('') const [locale, setLocale] = useState('') const [lat, setLatitude] = useState('') @@ -42,9 +43,19 @@ const LocationPage = ({ tab, setTab }: LocationPageProps) => { useEffect(() => { chrome.storage.local.get( - ['locationType', 'configuration', 'timezone', 'locale', 'lat', 'lon'], + [ + 'locationBrowserDefault', + 'configuration', + 'timezone', + 'locale', + 'lat', + 'lon', + ], (storage) => { - if (storage.locationType === 'matchIp' && ipData) { + storage.configuration && setConfiguration(storage.configuration) + storage.locationBrowserDefault !== undefined && + setBrowserDefault(storage.locationBrowserDefault) + if (storage.configuration === 'matchIp' && ipData) { setTimezone(ipData.timezone) setLocale(countryLocales[ipData.countryCode].locale) setLatitude(`${ipData.lat}`) @@ -55,37 +66,31 @@ const LocationPage = ({ tab, setTab }: LocationPageProps) => { lat: ipData.lat, lon: ipData.lon, }) - } else if (storage.locationType === 'custom') { - storage.configuration && setConfiguration(storage.configuration) + } else { storage.timezone && setTimezone(storage.timezone) storage.locale && setLocale(storage.locale) storage.lat && setLatitude(storage.lat) storage.lon && setLongitude(storage.lon) } - storage.locationType - ? setLocationType(storage.locationType) - : setLocationType('default') } ) }, [ipData]) - const changeType = (e: ChangeEvent) => { + const changeBrowserDefault = () => { detachDebugger() - setLocationType(e.target.value) - chrome.storage.local.set({ locationType: e.target.value }) + chrome.storage.local.set({ + locationBrowserDefault: !browserDefault, + }) + setBrowserDefault(!browserDefault) + } - if (e.target.value === 'default') { - setTimezone('') - setLocale('') - setLatitude('') - setLongitude('') - chrome.storage.local.set({ - timezone: '', - locale: '', - lat: '', - lon: '', - }) - } else if (e.target.value === 'matchIp') { + const changeConfiguration = (e: ChangeEvent) => { + detachDebugger() + setConfiguration(e.target.value) + chrome.storage.local.set({ + configuration: e.target.value, + }) + if (e.target.value === 'matchIp') { if (ipData) { setTimezone(ipData.timezone) setLocale(countryLocales[ipData.countryCode].locale) @@ -99,28 +104,17 @@ const LocationPage = ({ tab, setTab }: LocationPageProps) => { }) } } else if (e.target.value === 'custom') { - if (configuration !== 'custom') { - setTimezone(configurations[configuration].timezone) - setLocale(configurations[configuration].locale) - setLatitude(configurations[configuration].lat) - setLongitude(configurations[configuration].lon) - chrome.storage.local.set({ - timezone: configurations[configuration].timezone, - locale: configurations[configuration].locale, - lat: configurations[configuration].lat, - lon: configurations[configuration].lon, - }) - } - } - } - - const changeConfiguration = (e: ChangeEvent) => { - detachDebugger() - setConfiguration(e.target.value) - chrome.storage.local.set({ - configuration: e.target.value, - }) - if (e.target.value !== 'custom') { + setTimezone('') + setLocale('') + setLatitude('') + setLongitude('') + chrome.storage.local.set({ + timezone: '', + locale: '', + lat: '', + lon: '', + }) + } else { setTimezone(configurations[e.target.value].timezone) setLocale(configurations[e.target.value].locale) setLatitude(configurations[e.target.value].lat) @@ -135,12 +129,10 @@ const LocationPage = ({ tab, setTab }: LocationPageProps) => { } const changeInputText = () => { - if (locationType !== 'custom' || configuration !== 'custom') { + if (configuration !== 'custom') { setConfiguration('custom') - setLocationType('custom') chrome.storage.local.set({ configuration: 'custom', - locationType: 'custom', }) } } @@ -155,41 +147,17 @@ const LocationPage = ({ tab, setTab }: LocationPageProps) => { return ( - + - - - - - {locationType === 'matchIp' && ( { }} /> - )} - {locationType === 'custom' && ( - <> - - + + + {Object.keys(configurations).map((key) => ( ))} - - - )} - - - - + + + + + + + + + { }, []) const changeBrowserDefault = () => { - if (!browserDefault) { - detachDebugger() - } - + detachDebugger() chrome.storage.local.set({ userAgentBrowserDefault: !browserDefault, }) @@ -74,7 +71,6 @@ const UserAgentPage = ({ tab }: UserAgentPageProps) => { onChange={changeBrowserDefault} checked={browserDefault} /> - { 'localeMatchIP', 'userAgent', 'platform', + 'locationBrowserDefault', 'userAgentBrowserDefault', ], (storage) => { @@ -21,54 +22,56 @@ const attachDebugger = (tabId: number) => { storage.lon || storage.locale || storage.userAgent) && - !storage.userAgentBrowserDefault + (!storage.locationBrowserDefault || !storage.userAgentBrowserDefault) ) { chrome.debugger.attach({ tabId: tabId }, '1.3', () => { if (!chrome.runtime.lastError) { - if (storage.timezone) { - chrome.debugger.sendCommand( - { tabId: tabId }, - 'Emulation.setTimezoneOverride', - { - timezoneId: storage.timezone, - }, - () => { - if ( - chrome.runtime.lastError && - chrome.runtime.lastError.message?.includes( - 'Timezone override is already in effect' - ) - ) { - chrome.debugger.detach({ tabId }) + if (!storage.locationBrowserDefault) { + if (storage.timezone) { + chrome.debugger.sendCommand( + { tabId: tabId }, + 'Emulation.setTimezoneOverride', + { + timezoneId: storage.timezone, + }, + () => { + if ( + chrome.runtime.lastError && + chrome.runtime.lastError.message?.includes( + 'Timezone override is already in effect' + ) + ) { + chrome.debugger.detach({ tabId }) + } } - } - ) - } + ) + } - if (storage.locale) { - chrome.debugger.sendCommand( - { tabId: tabId }, - 'Emulation.setLocaleOverride', - { - locale: storage.locale, - } - ) - } + if (storage.locale) { + chrome.debugger.sendCommand( + { tabId: tabId }, + 'Emulation.setLocaleOverride', + { + locale: storage.locale, + } + ) + } - if (storage.lat || storage.lon) { - chrome.debugger.sendCommand( - { tabId: tabId }, - 'Emulation.setGeolocationOverride', - { - latitude: storage.lat - ? parseFloat(storage.lat) - : storage.ipData.lat, - longitude: storage.lon - ? parseFloat(storage.lon) - : storage.ipData.lon, - accuracy: 1, - } - ) + if (storage.lat || storage.lon) { + chrome.debugger.sendCommand( + { tabId: tabId }, + 'Emulation.setGeolocationOverride', + { + latitude: storage.lat + ? parseFloat(storage.lat) + : storage.ipData.lat, + longitude: storage.lon + ? parseFloat(storage.lon) + : storage.ipData.lon, + accuracy: 1, + } + ) + } } if (