diff --git a/entrypoints/background.ts b/entrypoints/background.ts index bf467d8..5220346 100644 --- a/entrypoints/background.ts +++ b/entrypoints/background.ts @@ -65,7 +65,7 @@ async function initTab(tabId: number, url: string, resolveDns = false) { }; tabStates.set(tabId, newState); - StorageService.setTabState(tabId, newState).catch(() => { }); + await StorageService.setTabState(tabId, newState).catch(() => { }); applyIconForState(tabId, newState); @@ -137,7 +137,7 @@ async function processIp(tabId: number, url: string, ip: string) { }; tabStates.set(tabId, newState); - await StorageService.setTabState(tabId, newState); + await StorageService.setTabState(tabId, newState).catch(() => { }); applyIconForState(tabId, newState); } @@ -280,6 +280,9 @@ export default defineBackground({ const state = tabStates.get(tab.id!); if (state) { applyIconForState(tab.id!, state); + if (state.status === 'loading' && Date.now() - state.lastUpdated > 2000) { + initTab(tab.id!, tab.url, true) + } } else { initTab(tab.id!, tab.url, true); } @@ -288,9 +291,19 @@ export default defineBackground({ browser.tabs.onUpdated.addListener((tabId, changeInfo, tab) => { if (changeInfo.status && tab.url) { - const state = tabStates.get(tabId); - if (state) { - applyIconForState(tabId, state); + if (tab.url) { + const state = tabStates.get(tabId); + if (state) { + applyIconForState(tabId, state); + if (!state || state.url !== tab.url) { + initTab(tabId, tab.url, true) + } else { + applyIconForState(tabId, state); + if (changeInfo.status === 'complete' && state.status === 'loading') { + initTab(tabId, tab.url, true) + } + } + } } } }); diff --git a/hooks/useHostInfo.ts b/hooks/useHostInfo.ts index b4fe64e..6d8fd28 100644 --- a/hooks/useHostInfo.ts +++ b/hooks/useHostInfo.ts @@ -19,6 +19,9 @@ export function useHostInfo() { setInfo(data); setLoading(false); } + if (data.status === 'loading' && Date.now() - data.lastUpdated > 2000) { + browser.runtime.sendMessage({ type: 'INIT_TAB', tabId: tab.id, url: tab.url }) + } } else { if (tab.url) { await browser.runtime.sendMessage({ type: 'INIT_TAB', tabId: tab.id, url: tab.url }); @@ -35,7 +38,15 @@ export function useHostInfo() { const listener = (changes: any, areaName: string) => { if (areaName === 'session' || areaName === 'local') { - fetchInfo(); + browser.tabs.query({ active: true, currentWindow: true }).then(([tab]) => { + if (tab?.id) { + const sessionKey = `tab_${tab.id}`; + const localKey = `session_tab_${tab.id}`; + if (changes[sessionKey] || changes[localKey]) { + fetchInfo(); + } + } + }); } }; diff --git a/utils/ip.ts b/utils/ip.ts index 9cc8757..2d86bb7 100644 --- a/utils/ip.ts +++ b/utils/ip.ts @@ -1,4 +1,4 @@ -const ipv4Regex = /^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/; +const ipv4Regex = /^(?:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)\.){3}(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)$/; const ipv6Regex = /^(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,3}(:[0-9a-fA-F]{1,4}){1,4})|([0-9a-fA-F]{1,2}(:[0-9a-fA-F]{1,4}){1,5})|([0-9a-fA-F]{1,4}:)((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))$/; export const IpUtils = {