mirror of
https://github.com/skidoodle/hostinfo
synced 2026-04-28 17:47:36 +02:00
fix lookup
This commit is contained in:
@@ -74,15 +74,30 @@ async function initTab(tabId: number, url: string, resolveDns = false) {
|
||||
|
||||
applyIconForState(tabId, newState);
|
||||
|
||||
if (!isSystem && !hasExistingData && resolveDns) {
|
||||
if (!isSystem && !hasExistingData) {
|
||||
const performDnsFallback = async () => {
|
||||
const state = tabStates.get(tabId);
|
||||
if (state?.status !== 'loading' || state.url !== url) return;
|
||||
|
||||
const ip = await DnsService.resolve(domain);
|
||||
|
||||
const stateAfterDns = tabStates.get(tabId);
|
||||
if (stateAfterDns?.status !== 'loading' || stateAfterDns.url !== url) return;
|
||||
|
||||
if (ip) {
|
||||
await processIp(tabId, url, ip);
|
||||
} else {
|
||||
await updateState(tabId, {
|
||||
status: 'error',
|
||||
errorMessage: 'Could not resolve host'
|
||||
});
|
||||
}, url);
|
||||
}
|
||||
};
|
||||
|
||||
if (resolveDns) {
|
||||
await performDnsFallback();
|
||||
} else {
|
||||
setTimeout(performDnsFallback, 1500);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -132,13 +147,14 @@ async function processIp(tabId: number, url: string, ip: string) {
|
||||
applyIconForState(tabId, newState);
|
||||
}
|
||||
|
||||
async function updateState(tabId: number, updates: Partial<TabState>) {
|
||||
async function updateState(tabId: number, updates: Partial<TabState>, expectedUrl?: string) {
|
||||
let current = tabStates.get(tabId);
|
||||
if (!current) {
|
||||
current = await StorageService.getTabState(tabId) || undefined;
|
||||
}
|
||||
|
||||
if (current) {
|
||||
if (expectedUrl && current.url !== expectedUrl) return;
|
||||
const newState = { ...current, ...updates };
|
||||
tabStates.set(tabId, newState);
|
||||
await StorageService.setTabState(tabId, newState);
|
||||
@@ -194,13 +210,16 @@ export default defineBackground({
|
||||
}
|
||||
const ip = await DnsService.resolve(hostname);
|
||||
|
||||
const currentState = tabStates.get(details.tabId);
|
||||
if (currentState?.status !== 'loading' || currentState.url !== details.url) return;
|
||||
|
||||
if (ip) {
|
||||
await processIp(details.tabId, details.url, ip);
|
||||
} else {
|
||||
await updateState(details.tabId, {
|
||||
status: 'error',
|
||||
errorMessage: 'Could not resolve host'
|
||||
});
|
||||
}, details.url);
|
||||
}
|
||||
}
|
||||
});
|
||||
@@ -208,12 +227,23 @@ export default defineBackground({
|
||||
browser.webRequest.onErrorOccurred.addListener(
|
||||
async (details) => {
|
||||
if (details.type !== 'main_frame') return;
|
||||
if (details.error === 'net::ERR_ABORTED') return;
|
||||
if (details.error === 'net::ERR_ABORTED') {
|
||||
try {
|
||||
const tab = await browser.tabs.get(details.tabId);
|
||||
if (tab.url) {
|
||||
const currentState = tabStates.get(details.tabId);
|
||||
if (currentState && currentState.url !== tab.url) {
|
||||
initTab(tab.id!, tab.url, true);
|
||||
}
|
||||
}
|
||||
} catch { }
|
||||
return;
|
||||
}
|
||||
|
||||
await updateState(details.tabId, {
|
||||
status: 'error',
|
||||
errorMessage: details.error
|
||||
});
|
||||
}, details.url);
|
||||
},
|
||||
{ urls: ['<all_urls>'] }
|
||||
);
|
||||
|
||||
+5
-1
@@ -1,13 +1,17 @@
|
||||
export const DnsService = {
|
||||
async resolve(hostname: string): Promise<string | null> {
|
||||
try {
|
||||
const controller = new AbortController();
|
||||
const timeout = setTimeout(() => controller.abort(), 5000);
|
||||
const response = await fetch(
|
||||
`https://cloudflare-dns.com/dns-query?name=${hostname}&type=A`,
|
||||
{
|
||||
headers: { accept: 'application/dns-json' },
|
||||
credentials: 'omit'
|
||||
credentials: 'omit',
|
||||
signal: controller.signal,
|
||||
}
|
||||
);
|
||||
clearTimeout(timeout);
|
||||
|
||||
if (!response.ok) return null;
|
||||
|
||||
|
||||
+5
-1
@@ -13,11 +13,15 @@ export const GeoService = {
|
||||
if (cached) return cached;
|
||||
|
||||
try {
|
||||
const controller = new AbortController();
|
||||
const timeout = setTimeout(() => controller.abort(), 5000);
|
||||
const res = await fetch(`https://ip.albert.lol/${ip}`, {
|
||||
method: 'GET',
|
||||
cache: 'force-cache',
|
||||
credentials: 'omit'
|
||||
credentials: 'omit',
|
||||
signal: controller.signal,
|
||||
});
|
||||
clearTimeout(timeout);
|
||||
|
||||
if (!res.ok) throw new Error(`API Error ${res.status}`);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user