mirror of
https://github.com/movie-web/extension.git
synced 2025-09-13 18:13:25 +00:00
Functionality and state for popout
This commit is contained in:
30
src/hooks/useDomain.ts
Normal file
30
src/hooks/useDomain.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
import { useEffect, useState } from 'react';
|
||||
|
||||
import { makeUrlIntoDomain } from '~utils/domains';
|
||||
|
||||
function queryCurrentDomain(cb: (domain: string | null) => void) {
|
||||
chrome.tabs.query({ active: true, currentWindow: true }).then((tabs) => {
|
||||
const url = tabs[0]?.url;
|
||||
if (!url) cb(null);
|
||||
else cb(url);
|
||||
});
|
||||
}
|
||||
|
||||
export function useDomain(): null | string {
|
||||
const [domain, setDomain] = useState<string | null>(null);
|
||||
|
||||
useEffect(() => {
|
||||
queryCurrentDomain(setDomain);
|
||||
function listen() {
|
||||
queryCurrentDomain(setDomain);
|
||||
}
|
||||
chrome.tabs.onActivated.addListener(listen);
|
||||
chrome.tabs.onUpdated.addListener(listen);
|
||||
return () => {
|
||||
chrome.tabs.onActivated.removeListener(listen);
|
||||
chrome.tabs.onUpdated.removeListener(listen);
|
||||
};
|
||||
}, []);
|
||||
|
||||
return makeUrlIntoDomain(domain);
|
||||
}
|
Reference in New Issue
Block a user