export default defineNuxtPlugin(() => { if (process.client) { let deferredPrompt: any = null // Listen for PWA install prompt window.addEventListener('beforeinstallprompt', (e) => { console.log('[PWA] Install prompt available') e.preventDefault() deferredPrompt = e // Show custom install button or notification showInstallNotification() }) // Listen for successful installation window.addEventListener('appinstalled', () => { console.log('[PWA] App successfully installed') deferredPrompt = null // Hide install notification hideInstallNotification() // Show success message showInstallSuccess() }) // Function to trigger install const installPWA = async () => { if (!deferredPrompt) { console.log('[PWA] No install prompt available') return } try { deferredPrompt.prompt() const { outcome } = await deferredPrompt.userChoice if (outcome === 'accepted') { console.log('[PWA] User accepted install') } else { console.log('[PWA] User dismissed install') } deferredPrompt = null } catch (error) { console.error('[PWA] Install error:', error) } } // Show install notification const showInstallNotification = () => { // Create install notification if it doesn't exist if (!document.getElementById('pwa-install-notification')) { const notification = document.createElement('div') notification.id = 'pwa-install-notification' notification.innerHTML = `
🎵
¡Instalar RepoDructor!
Accede rápidamente a tu música
` // Add CSS animation const style = document.createElement('style') style.textContent = ` @keyframes slideIn { from { transform: translateX(100%); opacity: 0; } to { transform: translateX(0); opacity: 1; } } ` document.head.appendChild(style) document.body.appendChild(notification) // Add event listeners document.getElementById('pwa-install-btn')?.addEventListener('click', installPWA) document.getElementById('pwa-dismiss-btn')?.addEventListener('click', hideInstallNotification) } } // Hide install notification const hideInstallNotification = () => { const notification = document.getElementById('pwa-install-notification') if (notification) { notification.remove() } } // Show success message const showInstallSuccess = () => { const success = document.createElement('div') success.innerHTML = `
✅
¡App instalada!
RepoDructor está ahora en tu dispositivo
` document.body.appendChild(success) // Remove after 3 seconds setTimeout(() => { success.remove() }, 3000) } // Make install function globally available ;(window as any).installRepoDructorPWA = installPWA } })