Skip to content

Commit

Permalink
feat: add useCurrentTab
Browse files Browse the repository at this point in the history
  • Loading branch information
helciofranco committed Jan 9, 2025
1 parent 220c3de commit 1827973
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions packages/app/src/systems/CRX/hooks/useCurrentTab.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { useEffect, useState } from 'react';

export function useCurrentTab() {
const [origin, setOrigin] = useState<string | undefined>();
const [faviconUrl, setFaviconUrl] = useState<string | undefined>();

useEffect(() => {
if (!chrome?.tabs) return;

chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
const currentTab = tabs[0];

if (currentTab?.url) {
const faviconUrl = currentTab.favIconUrl;
setFaviconUrl(faviconUrl);

try {
const url = new URL(currentTab.url);
const port = url.port ? `:${url.port}` : '';
const result = `${url.protocol}//${url.hostname}${port}`;
setOrigin(result);
} catch (_e) {
setOrigin(undefined);
}
}
});
}, []);

return {
origin,
faviconUrl,
};
}

0 comments on commit 1827973

Please sign in to comment.