debugging PWA

This commit is contained in:
Tobi 2024-04-09 10:13:47 +02:00
parent 153f15b1c0
commit 37748a2206

View file

@ -76,8 +76,6 @@
color: #666; color: #666;
font-size: 12px; font-size: 12px;
} }
</style>
<style>
#installButton { #installButton {
display: block; display: block;
margin: 0 auto; margin: 0 auto;
@ -88,47 +86,38 @@
border: none; border: none;
border-radius: 5px; border-radius: 5px;
cursor: pointer; cursor: pointer;
margin-top: 20px;
} }
#install-instructions { #install-instructions {
display: none; display: none;
text-align: center; text-align: center;
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="display: none; text-align: center; padding: 20px;"> <h1>Exchange Rate Calculator</h1>
<!-- Installation instructions (hidden by default) -->
<div id="install-instructions">
<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 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;"> <!-- Installation button (hidden by default) -->
Install App <button id="installButton" onclick="installApp()">Install App</button>
</button>
<h1>Exchange Rate Calculator</h1> <form onsubmit="convert(event)">
<form> <label for="euro">Enter amount in Euro:</label>
<label>Enter amount in Euro:</label>
<input type="number" id="euro" placeholder="Enter amount in Euro"> <input type="number" id="euro" placeholder="Enter amount in Euro">
<label>Enter amount in Algerian Dinar:</label>
<label for="dinar">Enter amount in Algerian Dinar:</label>
<input type="number" id="dinar" placeholder="Enter amount in Algerian Dinar"> <input type="number" id="dinar" placeholder="Enter amount in Algerian Dinar">
<button type="submit" onclick="convert(event)">Convert</button>
<button type="submit">Convert</button>
</form> </form>
<p class="result" id="result"></p> <p class="result" id="result"></p>
<div id="recentCalculations"> <div id="recentCalculations">
@ -158,12 +147,12 @@
if (euro) { if (euro) {
const result = (euro * exchangeRate).toFixed(2); const result = (euro * exchangeRate).toFixed(2);
const calculation = `${euro} Euro = ${result} Algerian Dinar`; const calculation = `${euro} Euro = ${result} Algerian Dinar`;
document.getElementById("result").innerHTML = calculation; document.getElementById("result").textContent = calculation;
recentCalculations.unshift(calculation); recentCalculations.unshift(calculation);
} else if (dinar) { } else if (dinar) {
const result = (dinar / exchangeRate).toFixed(2); const result = (dinar / exchangeRate).toFixed(2);
const calculation = `${dinar} Algerian Dinar = ${result} Euro`; const calculation = `${dinar} Algerian Dinar = ${result} Euro`;
document.getElementById("result").innerHTML = calculation; document.getElementById("result").textContent = calculation;
recentCalculations.unshift(calculation); recentCalculations.unshift(calculation);
} }
recentCalculations = recentCalculations.slice(0, 5); recentCalculations = recentCalculations.slice(0, 5);
@ -171,7 +160,7 @@
}) })
.catch(error => { .catch(error => {
console.log(error); console.log(error);
document.getElementById("result").innerHTML = "Oops, something went wrong! Please try again later."; document.getElementById("result").textContent = "Oops, something went wrong! Please try again later.";
}); });
} }
@ -185,51 +174,24 @@
calculationList.appendChild(li); calculationList.appendChild(li);
}); });
} }
</script>
<script> function installApp() {
if ('serviceWorker' in navigator) { if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('/service-worker.js') navigator.serviceWorker.register('/service-worker.js')
.then(function(registration) { .then(registration => {
console.log('Service Worker registered with scope:', registration.scope); console.log('Service Worker registered with scope:', registration.scope);
}) })
.catch(function(error) { .catch(error => {
console.error('Service Worker registration failed:', error); console.error('Service Worker registration failed:', error);
}); });
}
// Show installation instructions and button only if not already installed
if (!window.matchMedia('(display-mode: standalone)').matches) {
document.getElementById('install-instructions').style.display = 'block';
document.getElementById('installButton').style.display = 'block';
}
} }
</script> </script>
<script>
let deferredPrompt;
window.addEventListener('beforeinstallprompt', (event) => {
// Prevent the default installation prompt
event.preventDefault();
// Stash the event so it can be triggered later
deferredPrompt = event;
// Show installation instructions only if app is not already installed
if (!window.matchMedia('(display-mode: standalone)').matches) {
document.getElementById('install-instructions').classList.add('show');
document.getElementById('installButton').style.display = 'block';
}
});
function installApp() {
// Show the installation prompt when the user clicks the button
if (deferredPrompt) {
deferredPrompt.prompt();
deferredPrompt.userChoice.then((choiceResult) => {
if (choiceResult.outcome === 'accepted') {
console.log('User accepted the A2HS prompt');
// App installed, hide installation instructions
document.getElementById('install-instructions').classList.remove('show');
document.getElementById('installButton').style.display = 'none';
}
deferredPrompt = null;
});
}
}
</script>
</body> </body>
</html> </html>