mirror of
https://github.com/skidoodle/hostinfo
synced 2025-03-16 13:59:38 +01:00
almost there
This commit is contained in:
parent
38e6f714cb
commit
5eb326de05
273 changed files with 230 additions and 261 deletions
|
@ -1,7 +1,7 @@
|
|||
export function useTabData() {
|
||||
const [data, setData] = useState<ServerData | null>(null);
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
const [data, setData] = useState<ServerData | null>(null)
|
||||
const [loading, setLoading] = useState(true)
|
||||
const [error, setError] = useState<string | null>(null)
|
||||
|
||||
useEffect(() => {
|
||||
const fetchData = async () => {
|
||||
|
@ -9,14 +9,13 @@ export function useTabData() {
|
|||
const [tab] = await chrome.tabs.query({
|
||||
active: true,
|
||||
currentWindow: true,
|
||||
});
|
||||
})
|
||||
|
||||
if (!tab?.url) throw new Error('No active tab found');
|
||||
if (!tab?.url) throw new Error('No active tab found')
|
||||
|
||||
const url = new URL(tab.url);
|
||||
const hostname = url.hostname;
|
||||
const url = new URL(tab.url)
|
||||
const hostname = url.hostname
|
||||
|
||||
// Check for browser resources
|
||||
if (['chrome:', 'about:', 'file:'].includes(url.protocol)) {
|
||||
return setData({
|
||||
origin: '',
|
||||
|
@ -26,12 +25,11 @@ export function useTabData() {
|
|||
city: '',
|
||||
org: '',
|
||||
isLocal: false,
|
||||
isBrowserResource: true
|
||||
});
|
||||
isBrowserResource: true,
|
||||
})
|
||||
}
|
||||
|
||||
// Check for internal IPs
|
||||
const isInternal = isPrivateIP(hostname);
|
||||
const isInternal = isPrivateIP(hostname)
|
||||
if (isInternal) {
|
||||
return setData({
|
||||
origin: '',
|
||||
|
@ -41,41 +39,39 @@ export function useTabData() {
|
|||
city: '',
|
||||
org: '',
|
||||
isLocal: true,
|
||||
isBrowserResource: false
|
||||
});
|
||||
isBrowserResource: false,
|
||||
})
|
||||
}
|
||||
|
||||
const response = await chrome.runtime.sendMessage({
|
||||
type: 'FETCH_SERVER_INFO',
|
||||
hostname: hostname
|
||||
});
|
||||
hostname: hostname,
|
||||
})
|
||||
|
||||
if (!response) {
|
||||
throw new Error('No response from background script');
|
||||
throw new Error('No response from background script')
|
||||
}
|
||||
|
||||
// Handle API errors
|
||||
if (response.error) {
|
||||
throw new Error(response.error);
|
||||
throw new Error(response.error)
|
||||
}
|
||||
|
||||
// Validate response data
|
||||
if (!response.data?.ip) {
|
||||
throw new Error('Invalid server data received');
|
||||
throw new Error('Invalid server data received')
|
||||
}
|
||||
|
||||
setData(response.data);
|
||||
setError(null);
|
||||
setData(response.data)
|
||||
setError(null)
|
||||
} catch (err) {
|
||||
setError(err instanceof Error ? err.message : 'Failed to fetch data');
|
||||
setData(null);
|
||||
setError(err instanceof Error ? err.message : 'Failed to fetch data')
|
||||
setData(null)
|
||||
} finally {
|
||||
setLoading(false);
|
||||
setLoading(false)
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
fetchData();
|
||||
}, []);
|
||||
fetchData()
|
||||
}, [])
|
||||
|
||||
return { data, loading, error };
|
||||
return { data, loading, error }
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue