From c350da3cc32c1c68bbdcc35e6c0236969ff17c91 Mon Sep 17 00:00:00 2001 From: z0ccc Date: Fri, 6 May 2022 13:01:20 -0400 Subject: [PATCH] Added profile options --- src/pages/Background/index.js | 17 ++++- src/pages/Popup/DebugSettings.js | 72 +++++++++------------ src/pages/Popup/LocaleSettings.js | 2 + src/pages/Popup/Popup.jsx | 94 ++++++++++++++++++++++++++-- src/pages/Popup/UserAgentSettings.js | 21 +++---- src/pages/Popup/index.css | 2 +- src/utils/profiles.js | 11 ++++ webpack.config.js | 24 +++---- 8 files changed, 166 insertions(+), 77 deletions(-) create mode 100644 src/utils/profiles.js diff --git a/src/pages/Background/index.js b/src/pages/Background/index.js index 81304b2..884a17a 100644 --- a/src/pages/Background/index.js +++ b/src/pages/Background/index.js @@ -1,4 +1,4 @@ -const attachTab = (tabId, ipData) => { +const attachTab = (tabId) => { chrome.storage.sync.get( [ 'ipData', @@ -20,11 +20,11 @@ const attachTab = (tabId, ipData) => { result.locale || result.userAgent ) { - chrome.debugger.attach({ tabId: tabId }, '1.3', function () { + chrome.debugger.attach({ tabId: tabId }, '1.3', () => { if (!chrome.runtime.lastError) { // chrome.debugger.sendCommand( // { tabId: tabId }, - // 'Emulation.clearGeolocationOverride' + // 'Emulation.clearTimezoneOverride' // ) if (result.timezone) { @@ -33,6 +33,17 @@ const attachTab = (tabId, ipData) => { 'Emulation.setTimezoneOverride', { timezoneId: result.timezone, + }, + () => { + if ( + chrome.runtime.lastError && + chrome.runtime.lastError.message.includes( + 'Timezone override is already in effect' + ) + ) { + chrome.debugger.detach({ tabId }) + attachTab(tabId) + } } ) } diff --git a/src/pages/Popup/DebugSettings.js b/src/pages/Popup/DebugSettings.js index 6059dca..74229cc 100644 --- a/src/pages/Popup/DebugSettings.js +++ b/src/pages/Popup/DebugSettings.js @@ -1,43 +1,32 @@ import React, { useState, useEffect } from 'react' -import detachDebugger from '../../utils/detachDebugger' +import profiles from '../../utils/profiles' -const DebugSettings = ({ type, title, ip }) => { +const DebugSettings = ({ type, title, ip, profile, setProfile }) => { const [value, setValue] = useState('') - const [matchIP, setMatchIP] = useState(false) - const matchIPStorage = `${type}MatchIP` useEffect(() => { - if (ip) { - chrome.storage.sync.get([type, matchIPStorage], (result) => { - result[matchIPStorage] && setMatchIP(result[matchIPStorage]) - - if (result[matchIPStorage]) { - setValue(ip[type]) - chrome.storage.sync.set({ [type]: ip[type] }) - } else if (result[type]) { - setValue(result[type]) - } + if (profile === 'match') { + console.log(ip) + if (ip) { + setValue(ip[type]) + chrome.storage.sync.set({ [type]: ip[type] }) + } + } else if (profile === 'custom') { + console.log(111) + chrome.storage.sync.get([type], (result) => { + result[type] && setValue(result[type]) }) + } else { + setValue(profiles[profile][type]) + chrome.storage.sync.set({ [type]: profiles[profile][type] }) } - }, [ip, matchIPStorage, type]) + }, [ip, profile, type]) const changeTextValue = (e) => { chrome.storage.sync.set({ [type]: e.target.value }) setValue(e.target.value) - if (matchIP) { - chrome.storage.sync.set({ [matchIPStorage]: !matchIP }) - setMatchIP(!matchIP) - } - // detachDebugger() - } - - const toggleMatchIP = (e) => { - chrome.storage.sync.set({ - [type]: ip[type], - [matchIPStorage]: !matchIP, - }) - !matchIP && setValue(ip[type]) - setMatchIP(e.target.checked) + chrome.storage.sync.set({ profile: 'custom' }) + setProfile('custom') } return ( @@ -48,22 +37,19 @@ const DebugSettings = ({ type, title, ip }) => { margin: '12px 0 0 0', }} > - - */} ) } diff --git a/src/pages/Popup/LocaleSettings.js b/src/pages/Popup/LocaleSettings.js index 681d3dd..e27cdd9 100644 --- a/src/pages/Popup/LocaleSettings.js +++ b/src/pages/Popup/LocaleSettings.js @@ -1,5 +1,6 @@ import React, { useState, useEffect, useRef } from 'react' import countryLocales from '../../utils/countryLocales' +import detachDebugger from '../../utils/detachDebugger' const LocaleSettings = ({ ip }) => { const [value, setValue] = useState('') @@ -30,6 +31,7 @@ const LocaleSettings = ({ ip }) => { chrome.storage.sync.set({ localeMatchIP: !matchIP }) setMatchIP(!matchIP) } + detachDebugger() } const toggleMatchIP = (e) => { diff --git a/src/pages/Popup/Popup.jsx b/src/pages/Popup/Popup.jsx index 9eb4bd7..1fcd366 100644 --- a/src/pages/Popup/Popup.jsx +++ b/src/pages/Popup/Popup.jsx @@ -2,7 +2,7 @@ import React, { useState, useEffect } from 'react' import Navbar from './Navbar' import IpSettings from './IpSettings' import DebugSettings from './DebugSettings' -import LocaleSettings from './LocaleSettings' +import detachDebugger from '../../utils/detachDebugger' import UserAgentSettings from './UserAgentSettings' const getIP = () => @@ -15,9 +15,11 @@ const getIP = () => const Popup = () => { const [ip, setIP] = useState(null) + const [profile, setProfile] = useState('none') useEffect(() => { - chrome.storage.sync.get(['ipData'], (result) => { + chrome.storage.sync.get(['profile', 'ipData'], (result) => { + result.profile && setProfile(result.profile) if (result.ipData) { setIP(result.ipData) } else { @@ -26,6 +28,14 @@ const Popup = () => { }) }, []) + const changeProfile = (e) => { + detachDebugger() + chrome.storage.sync.set({ + profile: e.target.value, + }) + setProfile(e.target.value) + } + return (
@@ -35,10 +45,82 @@ const Popup = () => { }} > - - - - +
+ + +
+ + + + + {/* */}
{ margin: '12px 0 0 0', }} > - + +