60 lines
1.8 KiB
JavaScript
60 lines
1.8 KiB
JavaScript
// Register Service Worker for PWA
|
|
if ('serviceWorker' in navigator) {
|
|
window.addEventListener('load', () => {
|
|
navigator.serviceWorker.register('./sw.js')
|
|
.then((registration) => {
|
|
console.log('SW registered: ', registration);
|
|
})
|
|
.catch((registrationError) => {
|
|
console.log('SW registration failed: ', registrationError);
|
|
});
|
|
});
|
|
}
|
|
|
|
// PWA Install Prompt Logic
|
|
let deferredPrompt;
|
|
const installBtn = document.getElementById('installBtn');
|
|
|
|
window.addEventListener('beforeinstallprompt', (e) => {
|
|
// Prevent Chrome 67 and earlier from automatically showing the prompt
|
|
e.preventDefault();
|
|
// Stash the event so it can be triggered later.
|
|
deferredPrompt = e;
|
|
// Update UI to notify the user they can add to home screen
|
|
if(installBtn) {
|
|
installBtn.hidden = false;
|
|
}
|
|
});
|
|
|
|
if(installBtn) {
|
|
installBtn.addEventListener('click', async () => {
|
|
if (deferredPrompt !== null) {
|
|
// Show the install prompt
|
|
deferredPrompt.prompt();
|
|
// Wait for the user to respond to the prompt
|
|
const { outcome } = await deferredPrompt.userChoice;
|
|
if (outcome === 'accepted') {
|
|
console.log('User accepted the install prompt');
|
|
} else {
|
|
console.log('User dismissed the install prompt');
|
|
}
|
|
// We've used the prompt, and can't use it again, throw it away
|
|
deferredPrompt = null;
|
|
installBtn.hidden = true;
|
|
}
|
|
});
|
|
}
|
|
|
|
// Interactive element animation
|
|
const pulseElement = document.getElementById('pulseElement');
|
|
if(pulseElement) {
|
|
pulseElement.addEventListener('click', () => {
|
|
pulseElement.style.transform = 'scale(0.9)';
|
|
setTimeout(() => {
|
|
pulseElement.style.transform = 'scale(1)';
|
|
const randomColor = Math.floor(Math.random()*16777215).toString(16);
|
|
pulseElement.style.background = `#${randomColor}`;
|
|
}, 150);
|
|
});
|
|
}
|