Skip to content

Instantly share code, notes, and snippets.

@adactio
Created August 3, 2018 09:31
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adactio/cad5459210213c7a35f250c736a498dd to your computer and use it in GitHub Desktop.
Save adactio/cad5459210213c7a35f250c736a498dd to your computer and use it in GitHub Desktop.
Show a dismissable option to add The Session to the home screen (only shown to logged in users).
(function (win, doc) {
win.addEventListener('beforeinstallprompt', function (e) {
e.preventDefault();
var deferredPrompt = e;
var insertionPoint = doc.querySelector('main .contents');
insertionPoint.insertAdjacentHTML('afterbegin',`
<div class="feedback" role="dialog" aria-labelledby="homescreen">
<h2 id="homescreen">Install The Session</h2>
<p>Would you like to add The Session to your home screen?</p>
<button class="back">No, thanks</button>
<button class="aside">Yes, please!</button>
</div>
<br>
`);
var dialog = insertionPoint.querySelector('.feedback');
dialog.querySelector('button.back').addEventListener('click', function (e) {
dialog.remove();
doc.cookie = 'addtohomescreen=false;expires="Tue, 19 Jan 2038 03:14:07 GMT";path=/';
}, false);
dialog.querySelector('button.aside').addEventListener('click', function (e) {
deferredPrompt.prompt();
deferredPrompt.userChoice.then(function (choiceResult) {
if (choiceResult.outcome === 'accepted') {
dialog.remove();
doc.cookie = 'addtohomescreen=true;expires="Tue, 19 Jan 2038 03:14:07 GMT";path=/';
}
});
}, false);
}, false);
}(this, this.document));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment