Spaces:
Running
Running
| 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); | |