Spaces:
Sleeping
Sleeping
File size: 2,089 Bytes
26ab438 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
import { view } from './view.js';
view.init();
// Register service worker
if ('serviceWorker' in navigator) {
window.addEventListener('load', () => {
navigator.serviceWorker.register('service-worker.js')
.then(registration => {
console.log('Service Worker registered with scope: ', registration.scope);
})
.catch(err => {
console.log('Service Worker registration failed: ', err);
});
});
}
const button = document.createElement('button');
button.id = 'clearCacheButton';
button.textContent = 'v0.2';
// Set the button styles
Object.assign(button.style, {
position: 'fixed',
bottom: '0px',
right: '0px',
padding: '10px 20px',
backgroundColor: '#f44336', // Red background
color: 'white', // White text
border: 'none',
borderRadius: '5px',
cursor: 'pointer',
boxShadow: '0px 4px 6px rgba(0, 0, 0, 0.1)'
});
// Add click event to clear cache
button.addEventListener('click', () => {
if (window.confirm("updated?")) {
caches.keys().then(names => {
for (let name of names)
caches.delete(name);
});
// Get all registered service workers
navigator.serviceWorker.getRegistrations().then(function (registrations) {
// Loop through all registrations
for (let registration of registrations) {
// Check if the service worker's scope matches the desired scope
if (registration.scope === '/service-worker.js') {
// Unregister the service worker
registration.unregister().then(function () {
console.log('Service worker unregistered.');
}).catch(function (error) {
console.error('Error unregistering service worker:', error);
});
break;
}
}
});
alert('updated!');
}
});
// Append the button to the container
document.body.appendChild(button);
|