diff --git a/src/plugins/built-in/notificationCollector/index.ts b/src/plugins/built-in/notificationCollector/index.ts index 9763f4dd..dc4576c1 100644 --- a/src/plugins/built-in/notificationCollector/index.ts +++ b/src/plugins/built-in/notificationCollector/index.ts @@ -39,7 +39,7 @@ const notificationCollectorPlugin: Plugin<{}, NotificationCollectorStorage> = { "[class*='notifications__bubble___']", ) as HTMLElement; - if (api.storage.lastNotificationCount !== 0) { + if (alertDiv && api.storage.lastNotificationCount !== 0) { alertDiv.textContent = api.storage.lastNotificationCount.toString(); } @@ -63,7 +63,7 @@ const notificationCollectorPlugin: Plugin<{}, NotificationCollectorStorage> = { const notificationCount = data.payload.notifications.length; api.storage.lastNotificationCount = notificationCount; api.storage.lastCheckedTime = new Date().toISOString(); - + // Reset error count on success api.storage.consecutiveErrors = 0; @@ -74,31 +74,36 @@ const notificationCollectorPlugin: Plugin<{}, NotificationCollectorStorage> = { } } catch (error) { console.error("[BetterSEQTA+] Error fetching notifications:", error); - api.storage.consecutiveErrors = (api.storage.consecutiveErrors || 0) + 1; + api.storage.consecutiveErrors = + (api.storage.consecutiveErrors || 0) + 1; } }; const getNextInterval = () => { // Exponential backoff on errors, max 5 minutes - const errorMultiplier = Math.min(Math.pow(2, api.storage.consecutiveErrors || 0), 10); + const errorMultiplier = Math.min( + Math.pow(2, api.storage.consecutiveErrors || 0), + 10, + ); return Math.min(baseInterval * errorMultiplier, maxInterval); }; const startPolling = () => { if (pollInterval) return; // Already polling checkNotifications(); - + const scheduleNext = () => { const interval = getNextInterval(); pollInterval = window.setTimeout(() => { checkNotifications().then(() => { - if (pollInterval) { // Only continue if not stopped + if (pollInterval) { + // Only continue if not stopped scheduleNext(); } }); }, interval); }; - + scheduleNext(); }; @@ -124,14 +129,16 @@ const notificationCollectorPlugin: Plugin<{}, NotificationCollectorStorage> = { isVisible = !document.hidden; if (isVisible && !pollInterval) { // Resume polling when tab becomes visible - const alertDiv = document.querySelector("[class*='notifications__bubble___']"); + const alertDiv = document.querySelector( + "[class*='notifications__bubble___']", + ); if (alertDiv) { startPolling(); } } }; - document.addEventListener('visibilitychange', handleVisibilityChange); + document.addEventListener("visibilitychange", handleVisibilityChange); api.seqta.onMount("[class*='notifications__bubble___']", (_) => { startPolling(); @@ -139,7 +146,7 @@ const notificationCollectorPlugin: Plugin<{}, NotificationCollectorStorage> = { return () => { stopPolling(); - document.removeEventListener('visibilitychange', handleVisibilityChange); + document.removeEventListener("visibilitychange", handleVisibilityChange); }; }, };