Skip to content

Instantly share code, notes, and snippets.

@whoamindx
Forked from mnsmarcelo/app.js
Created August 17, 2018 18:57
Show Gist options
  • Save whoamindx/2b721dbd6fa851b47caaecd84e2a5470 to your computer and use it in GitHub Desktop.
Save whoamindx/2b721dbd6fa851b47caaecd84e2a5470 to your computer and use it in GitHub Desktop.
//referencia do prompt
var bannerInstall;
if ('serviceWorker' in navigator){
navigator.serviceWorker
.register('sw.js')
.then(function () {
console.log('Service worker está registrado!');
});
}
//evento disparado para instalar a aplicacao
window.addEventListener('beforeinstallprompt', function(event) {
console.log('beforeinstallprompt disparado...');
//prevenindo a instalacao e guardando a referencia
event.preventDefault();
bannerInstall = event;
return false;
});
// Controlando o banner de instalacao
var btnBannerInstall = document.querySelector('#enable-banner-install');
function showBannerInstall(){
if(bannerInstall){
//exibindo o banner
bannerInstall.prompt();
//verificando a escolha do usuario
bannerInstall.userChoice.then(function(choiceResult) {
if (choiceResult.outcome === 'dismissed') {
console.log('Usuario cancelou a instalacao');
} else {
console.log('Usuario aceitou a instalacao');
}
});
}
bannerInstall = null;
}
//clique do botao
btnBannerInstall.addEventListener('click', showBannerInstall);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment