|
| 1 | +import { storage } from '../../core/storage'; |
| 2 | +import { i18n } from '../../i18n/index'; |
| 3 | +import { renderOverlay, updateOverlayTheme } from './renderer'; |
| 4 | +import { watchOverlayPersistence } from './persistence'; |
| 5 | + |
| 6 | +/** |
| 7 | + * Main entry to show overlay |
| 8 | + */ |
| 9 | +export async function showOverlay(reasonKey: string = 'time'): Promise<void> { |
| 10 | + if (document.getElementById('limitra-overlay')) return; |
| 11 | + |
| 12 | + await i18n.init(); |
| 13 | + |
| 14 | + const toneKey = await storage.getQuoteTone(); |
| 15 | + const quoteText = i18n.getRandomQuote(toneKey); |
| 16 | + |
| 17 | + const reasonText = |
| 18 | + reasonKey === 'count' |
| 19 | + ? i18n.t.popup.reason_count |
| 20 | + : reasonKey === 'bypass' |
| 21 | + ? i18n.t.popup.reason_bypass |
| 22 | + : i18n.t.popup.reason_time; |
| 23 | + |
| 24 | + const badgeText = i18n.t.overlay.badgeBlocked; |
| 25 | + const unlocksInText = i18n.t.overlay.unlocksIn; |
| 26 | + const unlockingText = i18n.t.overlay.unlocking; |
| 27 | + const clickToCopyText = i18n.t.overlay.clickToCopy; |
| 28 | + const copiedText = i18n.t.overlay.copiedText; |
| 29 | + const direction = i18n.language === 'ar' ? 'rtl' : 'ltr'; |
| 30 | + |
| 31 | + const savedTheme = await storage.getTheme(); |
| 32 | + const isDark = |
| 33 | + savedTheme === 'dark' || |
| 34 | + (savedTheme === 'auto' && window.matchMedia('(prefers-color-scheme: dark)').matches); |
| 35 | + |
| 36 | + const nextResetTime = await storage.getNextResetTime(); |
| 37 | + |
| 38 | + renderOverlay({ |
| 39 | + badgeText, |
| 40 | + quoteText, |
| 41 | + reasonText, |
| 42 | + isDark, |
| 43 | + direction, |
| 44 | + nextResetTime, |
| 45 | + unlocksInText, |
| 46 | + unlockingText, |
| 47 | + clickToCopyText, |
| 48 | + copiedText, |
| 49 | + }); |
| 50 | + |
| 51 | + watchOverlayPersistence(() => { |
| 52 | + void showOverlay('bypass'); |
| 53 | + }); |
| 54 | +} |
| 55 | + |
| 56 | +/** |
| 57 | + * Setup listeners (call once) |
| 58 | + */ |
| 59 | +export function initOverlayListeners(): void { |
| 60 | + chrome.storage.onChanged.addListener((changes, namespace) => { |
| 61 | + if (namespace === 'local' && changes['limitra_theme']) { |
| 62 | + const theme = changes['limitra_theme'].newValue as string; |
| 63 | + const isDark = |
| 64 | + theme === 'dark' || |
| 65 | + (theme === 'auto' && window.matchMedia('(prefers-color-scheme: dark)').matches); |
| 66 | + |
| 67 | + updateOverlayTheme(isDark); |
| 68 | + } |
| 69 | + }); |
| 70 | + |
| 71 | + window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', async () => { |
| 72 | + const currentTheme = await storage.getTheme(); |
| 73 | + if (currentTheme === 'auto') { |
| 74 | + const isDark = window.matchMedia('(prefers-color-scheme: dark)').matches; |
| 75 | + updateOverlayTheme(isDark); |
| 76 | + } |
| 77 | + }); |
| 78 | +} |
0 commit comments