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