Advertisement
Guest User

Untitled

a guest
Dec 3rd, 2019
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. let deferredInstallPrompt = null;
  2. const installButton = document.getElementById('butInstall');
  3. installButton.addEventListener('click', installPWA);
  4.  
  5. window.addEventListener('beforeinstallprompt', saveBeforeInstallPromptEvent);
  6.  
  7.  
  8. function saveBeforeInstallPromptEvent(evt) {
  9.     deferredInstallPrompt = evt;
  10.     installButton.removeAttribute('hidden');
  11. }
  12.  
  13. function installPWA(evt) {
  14.     // CODELAB: Add code show install prompt & hide the install button.
  15.     deferredInstallPrompt.prompt();
  16.     // Escondendo botão
  17.     evt.srcElement.setAttribute('hidden', true);
  18.  
  19.     //Interceptando se o usuário aceitou ou não a instalação
  20.     deferredInstallPrompt.userChoice
  21.         .then((choice) => {
  22.             if (choice.outcome === 'accepted') {
  23.                 console.log('Usuário aceitou', choice);
  24.             } else {
  25.                 console.log('Usuário não aceitou', choice);
  26.             }
  27.             deferredInstallPrompt = null;
  28.         });
  29.  
  30. }
  31.  
  32. window.addEventListener('appinstalled', logAppInstalled);
  33.  
  34. function logAppInstalled(evt) {
  35.     console.log('Aplicativo já está instalado.', evt);
  36.  
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement