diff --git a/src/pages/Background/index.js b/src/pages/Background/index.js index bcade45..c861452 100644 --- a/src/pages/Background/index.js +++ b/src/pages/Background/index.js @@ -1,100 +1,24 @@ +import attachDebugger from '../../utils/attachDebugger' import userAgents from '../../utils/userAgents' -const attachTab = (tabId) => { - chrome.storage.sync.get( - [ - 'ipData', - 'timezone', - 'timezoneMatchIP', - 'lat', - 'latitudeMatchIP', - 'lon', - 'longitudeMatchIP', - 'locale', - 'localeMatchIP', - 'userAgent', - ], - (result) => { - if ( - result.timezone || - result.lat || - result.lon || - result.locale || - result.userAgent - ) { - chrome.debugger.attach({ tabId: tabId }, '1.3', () => { - if (!chrome.runtime.lastError) { - if (result.timezone) { - chrome.debugger.sendCommand( - { tabId: tabId }, - '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) - } - } - ) - } +chrome.tabs.onCreated.addListener((tab) => { + attachDebugger(tab.id) +}) - if (result.locale) { - chrome.debugger.sendCommand( - { tabId: tabId }, - 'Emulation.setLocaleOverride', - { - locale: result.locale, - } - ) - } - - if (result.lat || result.lon) { - chrome.debugger.sendCommand( - { tabId: tabId }, - 'Emulation.setGeolocationOverride', - { - latitude: result.lat - ? parseFloat(result.lat) - : result.ipData.lat, - longitude: result.lon - ? parseFloat(result.lon) - : result.ipData.lon, - accuracy: 1, - } - ) - } - - if (result.userAgent) { - chrome.debugger.sendCommand( - { tabId: tabId }, - 'Emulation.setUserAgentOverride', - { - userAgent: result.userAgent, - } - // { acceptLanguage: "en-CA" }, - // { platform: "WebTV OS" } - ) - // 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Safari/537.69', - } - } - }) - } +chrome.tabs.onActivated.addListener((tab) => { + chrome.debugger.getTargets((tabs) => { + const currentTab = tabs.find((obj) => obj.tabId === tab.tabId) + if (!currentTab.attached) { + attachDebugger(tab.tabId) } - ) -} + }) +}) -chrome.tabs.onUpdated.addListener((tabId, change, tab) => { +chrome.tabs.onUpdated.addListener((tabId) => { chrome.debugger.getTargets((tabs) => { const currentTab = tabs.find((obj) => obj.tabId === tabId) if (!currentTab.attached) { - attachTab(tabId) + attachDebugger(tabId) } }) }) @@ -103,7 +27,6 @@ chrome.alarms.onAlarm.addListener((alarm) => { if (alarm.name === 'userAgentAlarm') { chrome.storage.sync.get(['randomUA'], (result) => { if (result.randomUA) { - console.log('userAgentAlarm') const randomUserAgent = userAgents[Math.floor(Math.random() * userAgents.length)] chrome.storage.sync.set({ diff --git a/src/pages/Popup/Navbar.js b/src/pages/Popup/Navbar.js index 13ae723..77047a5 100644 --- a/src/pages/Popup/Navbar.js +++ b/src/pages/Popup/Navbar.js @@ -23,7 +23,7 @@ const Navbar = () => ( alt="Vytal logo" /> { fontSize: '10px', }} > - Tabs need to be initialized for full protection. + Current tabs won't be fully spoofed until after first or second reload. diff --git a/src/pages/Popup/index.css b/src/pages/Popup/index.css index d71bf63..f09bde7 100644 --- a/src/pages/Popup/index.css +++ b/src/pages/Popup/index.css @@ -13,7 +13,7 @@ body { background-color: var(--background); font-size: 13px; line-height: 22px; - width: 315px; + width: 325px; margin: 0; font-family: 'Segoe UI', Tahoma, sans-serif; } diff --git a/src/utils/attachDebugger.js b/src/utils/attachDebugger.js new file mode 100644 index 0000000..c0a586b --- /dev/null +++ b/src/utils/attachDebugger.js @@ -0,0 +1,90 @@ +const attachDebugger = (tabId) => { + chrome.storage.sync.get( + [ + 'ipData', + 'timezone', + 'timezoneMatchIP', + 'lat', + 'latitudeMatchIP', + 'lon', + 'longitudeMatchIP', + 'locale', + 'localeMatchIP', + 'userAgent', + ], + (result) => { + if ( + result.timezone || + result.lat || + result.lon || + result.locale || + result.userAgent + ) { + chrome.debugger.attach({ tabId: tabId }, '1.3', () => { + if (!chrome.runtime.lastError) { + if (result.timezone) { + chrome.debugger.sendCommand( + { tabId: tabId }, + 'Emulation.setTimezoneOverride', + { + timezoneId: result.timezone, + }, + () => { + if ( + chrome.runtime.lastError && + chrome.runtime.lastError.message.includes( + 'Timezone override is already in effect' + ) + ) { + chrome.debugger.detach({ tabId }) + } + } + ) + } + + if (result.locale) { + chrome.debugger.sendCommand( + { tabId: tabId }, + 'Emulation.setLocaleOverride', + { + locale: result.locale, + } + ) + } + + if (result.lat || result.lon) { + chrome.debugger.sendCommand( + { tabId: tabId }, + 'Emulation.setGeolocationOverride', + { + latitude: result.lat + ? parseFloat(result.lat) + : result.ipData.lat, + longitude: result.lon + ? parseFloat(result.lon) + : result.ipData.lon, + accuracy: 1, + } + ) + } + + if (result.userAgent) { + chrome.debugger.sendCommand( + { tabId: tabId }, + 'Emulation.setUserAgentOverride', + { + userAgent: result.userAgent, + } + // { acceptLanguage: "en-CA" }, + // { platform: "WebTV OS" } + ) + // 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Safari/537.69', + } + } + }) + } + } + ) +} + +export default attachDebugger diff --git a/src/utils/detachDebugger.js b/src/utils/detachDebugger.js index 75cf5ba..d62f743 100644 --- a/src/utils/detachDebugger.js +++ b/src/utils/detachDebugger.js @@ -1,4 +1,4 @@ -const detachDebugger = () => { +const detachDebugger = (attach) => { chrome.debugger.getTargets((tabs) => { for (const tab in tabs) { if (tabs[tab].attached && tabs[tab].tabId) {