Created
January 1, 2019 19:20
-
-
Save jsanta/d41cde281eba7c17f04417b27b2351e8 to your computer and use it in GitHub Desktop.
Defer installation script for PWAs (copy & pasted from https://developers.google.com/web/fundamentals/app-install-banners/ )
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <script type="text/javascript"> | |
| // ... | |
| // Ref. https://developers.google.com/web/fundamentals/app-install-banners/ | |
| function addToHomeScreen() { | |
| let a2hsBtn = document.querySelector(".ad2hs-prompt"); // hide our user interface that shows our A2HS button | |
| a2hsBtn.style.display = 'none'; // Show the prompt | |
| deferredPrompt.prompt(); // Wait for the user to respond to the prompt | |
| deferredPrompt.userChoice | |
| .then(function(choiceResult){ | |
| if (choiceResult.outcome === 'accepted') { | |
| console.log('User accepted the A2HS prompt'); | |
| } else { | |
| console.log('User dismissed the A2HS prompt'); | |
| } | |
| deferredPrompt = null; | |
| });} | |
| function showAddToHomeScreen() { | |
| let a2hsBtn = document.querySelector(".ad2hs-prompt"); | |
| a2hsBtn.style.display = "block"; | |
| a2hsBtn.addEventListener("click", addToHomeScreen); | |
| } | |
| let deferredPrompt; | |
| window.addEventListener('beforeinstallprompt', function (e) { | |
| // Prevent Chrome 67 and earlier from automatically showing the prompt | |
| e.preventDefault(); | |
| // Stash the event so it can be triggered later. | |
| deferredPrompt = e; | |
| showAddToHomeScreen(); | |
| }); | |
| </script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment