class PWAInstallPrompt { constructor() { this.deferredPrompt = null; this.isInstalled = false; this.installButton = null; this.installBanner = null; this.dismissedCount = 0; this.maxDismissals = 3; this.installationChecked = false; this.delayedPromptTimeout = null; this.init(); } init() { console.log('💿 PWA Install Prompt initializing...'); this.checkInstallationStatus(); this.setupEventListeners(); this.createInstallButton(); this.loadInstallPreferences(); // Проверяем статус установки периодически для iOS if (this.isIOSSafari()) { this.startInstallationMonitoring(); } // Автоматический показ модального окна через 10 секунд для новых пользователей this.scheduleDelayedPrompt(); console.log('✅ PWA Install Prompt initialized'); } checkInstallationStatus() { // Проверяем различные способы определения установки PWA const isStandalone = window.matchMedia('(display-mode: standalone)').matches; const isIOSStandalone = window.navigator.standalone === true; const hasInstallPreference = this.loadInstallPreferences().installed; // Проверяем, установлено ли приложение if (isStandalone || isIOSStandalone || hasInstallPreference) { this.isInstalled = true; console.log('📱 App is already installed as PWA'); document.body.classList.add('pwa-installed'); // Скрываем все промпты установки this.hideInstallPrompts(); // Если это iOS, добавляем специальный класс if (this.isIOSSafari()) { document.body.classList.add('ios-pwa'); } this.installationChecked = true; return true; } // Если не установлено, добавляем соответствующие классы document.body.classList.add('pwa-browser'); if (this.isIOSSafari()) { document.body.classList.add('ios-safari'); } this.installationChecked = true; return false; } startInstallationMonitoring() { // Для iOS Safari мониторим изменения в standalone режиме let wasStandalone = window.navigator.standalone; const checkStandalone = () => { const isStandalone = window.navigator.standalone; if (isStandalone && !wasStandalone && !this.isInstalled) { console.log('✅ iOS PWA installation detected'); this.isInstalled = true; this.hideInstallPrompts(); this.showInstallSuccess(); document.body.classList.remove('pwa-browser'); document.body.classList.add('pwa-installed', 'ios-pwa'); // Сохраняем предпочтение установки this.saveInstallPreference('installed', true); } wasStandalone = isStandalone; }; // Проверяем каждые 2 секунды setInterval(checkStandalone, 2000); // Также проверяем при изменении видимости страницы window.addEventListener('visibilitychange', () => { if (!document.hidden) { setTimeout(checkStandalone, 1000); } }); } scheduleDelayedPrompt() { // Автоматический показ модального окна через 10 секунд для новых пользователей this.delayedPromptTimeout = setTimeout(() => { console.log('⏰ Checking if delayed install prompt should be shown...'); // Проверяем, нужно ли показывать промпт if (!this.isInstalled && this.shouldShowPrompt()) { console.log('💿 Showing delayed install modal after 10 seconds'); // Для iOS Safari показываем модальное окно с инструкциями if (this.isIOSSafari()) { this.showIOSInstallInstructions(); } else { // Для других устройств показываем fallback инструкции this.showFallbackInstructions(); } } else { console.log('💿 Delayed install prompt not shown - app is installed or dismissed'); } }, 10000); // 10 секунд console.log('⏰ Delayed install prompt scheduled for 10 seconds'); } setupEventListeners() { window.addEventListener('beforeinstallprompt', (event) => { console.log('💿 Install prompt event captured'); event.preventDefault(); this.deferredPrompt = event; // Показываем промпт только если приложение не установлено if (!this.isInstalled && this.shouldShowPrompt()) { this.showInstallOptions(); } }); window.addEventListener('appinstalled', () => { console.log('✅ PWA installed successfully'); this.isInstalled = true; this.hideInstallPrompts(); this.showInstallSuccess(); this.saveInstallPreference('installed', true); document.body.classList.remove('pwa-browser'); document.body.classList.add('pwa-installed'); }); // Дополнительная проверка для iOS if (this.isIOSSafari()) { let wasStandalone = window.navigator.standalone; window.addEventListener('visibilitychange', () => { if (document.hidden) return; setTimeout(() => { const isStandalone = window.navigator.standalone; if (isStandalone && !wasStandalone && !this.isInstalled) { console.log('✅ iOS PWA installation detected'); this.isInstalled = true; this.hideInstallPrompts(); this.showInstallSuccess(); document.body.classList.remove('pwa-browser'); document.body.classList.add('pwa-installed', 'ios-pwa'); // Сохраняем предпочтение установки this.saveInstallPreference('installed', true); } wasStandalone = isStandalone; }, 1000); }); } } createInstallButton() { this.installButton = document.createElement('button'); this.installButton.id = 'pwa-install-button'; this.installButton.className = 'hidden fixed bottom-6 right-6 bg-gradient-to-r from-orange-500 to-orange-600 hover:from-orange-600 hover:to-orange-700 text-white px-6 py-3 rounded-full shadow-lg transition-all duration-300 z-50 flex items-center space-x-3 group'; const buttonText = this.isIOSSafari() ? 'Install App' : 'Install App'; const buttonIcon = this.isIOSSafari() ? 'fas fa-share' : 'fas fa-download'; this.installButton.innerHTML = ` ${buttonText}
`; this.installButton.addEventListener('click', () => { this.handleInstallClick(); }); document.body.appendChild(this.installButton); } createInstallBanner() { if (this.installBanner) return; this.installBanner = document.createElement('div'); this.installBanner.id = 'pwa-install-banner'; this.installBanner.className = 'pwa-install-banner fixed bottom-0 left-0 right-0 transform translate-y-full transition-transform duration-300 z-40'; this.installBanner.innerHTML = `After installation, open SecureBit from your home screen for the best experience.
To install this app, look for the install option in your browser menu or address bar. Different browsers have different install methods.