mirror of
https://github.com/movie-web/extension.git
synced 2025-09-13 18:13:25 +00:00
Support both firefox and chrome
This commit is contained in:
@@ -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);
|
||||
};
|
||||
}, []);
|
||||
|
||||
|
Reference in New Issue
Block a user