File size: 1,850 Bytes
91073d4 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
// Script pour gérer l'installation de l'application PWA
let deferredPrompt;
const installButton = document.getElementById('install-button');
const installContainer = document.getElementById('install-container');
// Cacher le bouton d'installation par défaut
if (installContainer) {
installContainer.style.display = 'none';
}
// Écouter l'événement 'beforeinstallprompt'
window.addEventListener('beforeinstallprompt', (e) => {
// Empêcher le mini-infobar de Chrome de s'afficher sur mobile
e.preventDefault();
// Stocker l'événement pour l'utiliser plus tard
deferredPrompt = e;
// Afficher le bouton d'installation
if (installContainer) {
installContainer.style.display = 'flex';
}
// Ajouter l'écouteur d'événement pour le bouton d'installation
if (installButton) {
installButton.addEventListener('click', () => {
// Cacher le bouton d'installation
installContainer.style.display = 'none';
// Afficher l'invite d'installation
deferredPrompt.prompt();
// Attendre que l'utilisateur réponde à l'invite
deferredPrompt.userChoice.then((choiceResult) => {
if (choiceResult.outcome === 'accepted') {
console.log('L\'utilisateur a accepté l\'installation');
} else {
console.log('L\'utilisateur a refusé l\'installation');
}
// Effacer la référence à l'événement
deferredPrompt = null;
});
});
}
});
// Écouter l'événement 'appinstalled'
window.addEventListener('appinstalled', (e) => {
// Cacher le bouton d'installation
if (installContainer) {
installContainer.style.display = 'none';
}
console.log('Application installée');
}); |