Skip to content

Instantly share code, notes, and snippets.

@russiann
Forked from mnsmarcelo/app.js
Created August 17, 2018 18:53
Show Gist options
  • Save russiann/c2f8ce6bfc86322627a3ef0f95f90b53 to your computer and use it in GitHub Desktop.
Save russiann/c2f8ce6bfc86322627a3ef0f95f90b53 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