Support both firefox and chrome

This commit is contained in:
mrjvs
2024-01-10 20:30:12 +01:00
parent ba83059d43
commit f831dea5d6
6 changed files with 133 additions and 54 deletions

View File

@@ -1,28 +1,17 @@
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);
});
}
import { listenToTabChanges, queryCurrentDomain, stopListenToTabChanges } from '~utils/tabs';
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);
const listen = () => queryCurrentDomain(setDomain);
listen();
listenToTabChanges(listen);
return () => {
chrome.tabs.onActivated.removeListener(listen);
chrome.tabs.onUpdated.removeListener(listen);
stopListenToTabChanges(listen);
};
}, []);