debugging PWA

This commit is contained in:
Tobi 2024-04-09 10:06:44 +02:00
parent c4629d777a
commit 153f15b1c0

View file

@ -96,14 +96,30 @@
padding: 20px; padding: 20px;
} }
</style> </style>
<style>
/* Center the button horizontally */
#installButton {
display: block;
}
/* Show installation instructions when needed */
#install-instructions.show {
display: block;
}
</style>
</head> </head>
<body> <body>
<div id="install-instructions" style="text-align: center; padding: 20px;"> <div id="install-instructions" style="display: none; text-align: center; padding: 20px;">
<p>For the best experience, add this web app to your home screen:</p> <p>For the best experience, add this web app to your home screen:</p>
<p>1. Tap the <strong>Share</strong> button.</p> <p>1. Tap the <strong>Share</strong> button.</p>
<p>2. Tap <strong>Add to Home Screen</strong>.</p> <p>2. Tap <strong>Add to Home Screen</strong>.</p>
</div> </div>
<button onclick="installApp()">Install App</button>
<button id="installButton" style="display: none; margin: 0 auto; padding: 10px 20px; font-size: 16px; background-color: #4CAF50; color: #fff; border: none; border-radius: 5px; cursor: pointer;">
Install App
</button>
<h1>Exchange Rate Calculator</h1> <h1>Exchange Rate Calculator</h1>
<form> <form>
@ -185,20 +201,27 @@
let deferredPrompt; let deferredPrompt;
window.addEventListener('beforeinstallprompt', (event) => { window.addEventListener('beforeinstallprompt', (event) => {
// Prevent the default installation prompt
event.preventDefault(); event.preventDefault();
// Stash the event so it can be triggered later
deferredPrompt = event; deferredPrompt = event;
// Show installation instructions only if app is not already installed
if (!window.matchMedia('(display-mode: standalone)').matches) { if (!window.matchMedia('(display-mode: standalone)').matches) {
document.getElementById('install-instructions').style.display = 'block'; document.getElementById('install-instructions').classList.add('show');
document.getElementById('installButton').style.display = 'block';
} }
}); });
function installApp() { function installApp() {
// Show the installation prompt when the user clicks the button
if (deferredPrompt) { if (deferredPrompt) {
deferredPrompt.prompt(); deferredPrompt.prompt();
deferredPrompt.userChoice.then((choiceResult) => { deferredPrompt.userChoice.then((choiceResult) => {
if (choiceResult.outcome === 'accepted') { if (choiceResult.outcome === 'accepted') {
console.log('User accepted the A2HS prompt'); console.log('User accepted the A2HS prompt');
document.getElementById('install-instructions').style.display = 'none'; // App installed, hide installation instructions
document.getElementById('install-instructions').classList.remove('show');
document.getElementById('installButton').style.display = 'none';
} }
deferredPrompt = null; deferredPrompt = null;
}); });
@ -206,6 +229,7 @@
} }
</script> </script>
</body> </body>
</html> </html>