function isPWA() { const isStandalone = window.matchMedia('(display-mode: standalone)').matches; const isIOSStandalone = window.navigator.standalone === true; // iOS return isStandalone || isIOSStandalone; } document.addEventListener('DOMContentLoaded', async () => { let deferredPrompt; const installPwaButtons = document.querySelectorAll('.install-pwa'); function hideInstallButtons() { installPwaButtons.forEach(button => { button.style.display = 'none' button.classList.remove('sm:block') }) } function showInstallButtons() { installPwaButtons.forEach(button => { button.style.display = 'block' button.classList.add('sm:block') }) } hideInstallButtons() window.addEventListener('beforeinstallprompt', event => { event.preventDefault(); deferredPrompt = event; showInstallButtons() installPwaButtons.forEach(button => button.addEventListener('click', () => { showInstallButtons() deferredPrompt.prompt(); deferredPrompt.userChoice.then(result => { if (result.outcome === 'accepted') { console.log('El usuario aceptó la instalación'); } else { console.log('El usuario rechazó la instalación'); } deferredPrompt = null; }); })) }) window.addEventListener('appinstalled', () => { hideInstallButtons() console.log('PWA ha sido instalada'); }); if (isPWA()) { hideInstallButtons() } if ('getInstalledRelatedApps' in navigator) { navigator.getInstalledRelatedApps().then((relatedApps) => { if (relatedApps.length > 0) { hideInstallButtons() } }); } })