import { fetchNotificationCount, getNotificationsUrl, getNotificationsUrlPattern, } from "@/utils/api"; import { renderCount, renderError } from "@/utils/badge"; import { notificationCount } from "@/utils/storage"; const POLL_INTERVAL_MINUTES = 1; const updateCount = async (): Promise => { try { const count = await fetchNotificationCount(); await notificationCount.setValue(count); renderCount(count); } catch { renderError(); } }; export default defineBackground(() => { const action = browser.action ?? browser.browserAction; browser.alarms.create("poll", { periodInMinutes: POLL_INTERVAL_MINUTES }); browser.alarms.onAlarm.addListener(() => updateCount()); action.onClicked.addListener(async () => { const tabs = await browser.tabs.query({ currentWindow: true, url: getNotificationsUrlPattern(), }); if (tabs[0]?.id) { browser.tabs.update(tabs[0].id, { active: true }); browser.tabs.reload(tabs[0].id); } else { browser.tabs.create({ url: getNotificationsUrl() }); } setTimeout(updateCount, 2000); }); updateCount(); });