diff --git a/packages/webapp/public/logo-maskable-144.png b/packages/webapp/public/logo-maskable-144.png index a14e6f97..13650b33 100644 Binary files a/packages/webapp/public/logo-maskable-144.png and b/packages/webapp/public/logo-maskable-144.png differ diff --git a/packages/webapp/public/manifest.webmanifest b/packages/webapp/public/manifest.webmanifest deleted file mode 100644 index e5b4b8d5..00000000 --- a/packages/webapp/public/manifest.webmanifest +++ /dev/null @@ -1,81 +0,0 @@ -{ - "id": "com.nesbox", - "name": "NESBox", - "short_name": "NESBox", - "categories": ["entertainment", "games"], - "description": "Enjoy game", - "scope": "/", - "start_url": "/?utm_source=web_app_manifest", - "background_color": "black", - "theme_color": "black", - "display": "standalone", - "orientation": "landscape", - "file_handlers": [ - { - "action": "/emulator", - "name": "NES file", - "accept": { - "application/octet-stream": [".nes"] - } - } - ], - "launch_handler": { - "route_to": "existing-client-retain" - }, - "shortcuts": [ - { - "name": "All Games", - "url": "/games" - }, - { - "name": "My Favorites", - "url": "/favorites" - } - ], - "icons": [ - { - "src": "/logo-32.png", - "sizes": "32x32", - "type": "image/png" - }, - { - "src": "/logo-96.png", - "sizes": "96x96", - "type": "image/png" - }, - { - "src": "/logo-maskable-144.png", - "sizes": "144x144", - "type": "image/png", - "purpose": "maskable" - }, - { - "src": "/logo-144.png", - "sizes": "144x144", - "type": "image/png" - } - ], - "screenshots": [ - { - "src": "https://raw.githubusercontent.com/mantou132/nesbox/master/screenshots/homepage.png", - "sizes": "1280x861", - "type": "image/png", - "platform": "wide", - "label": "Homescreen of NESBox" - }, - { - "src": "https://raw.githubusercontent.com/mantou132/nesbox/master/screenshots/playing.png", - "sizes": "1280x861", - "type": "image/png", - "platform": "wide", - "label": "Playing game in NESBox" - }, - { - "src": "https://raw.githubusercontent.com/mantou132/nesbox/master/screenshots/settings.png", - "sizes": "1280x861", - "type": "image/png", - "platform": "wide", - "label": "Settings in NESBox" - } - ] -} diff --git a/packages/webapp/public/sw.js b/packages/webapp/public/sw.js index b817bf3a..ee1254ac 100644 --- a/packages/webapp/public/sw.js +++ b/packages/webapp/public/sw.js @@ -8,7 +8,7 @@ const sw = self; const cacheName = 'console'; -const assetsToCache = [...new Set([sw.location.pathname, '/'])].map((path) => sw.location.origin + path); +const assetsToCache = [...new Set(['/'])].map((path) => location.origin + path); sw.addEventListener('activate', (event) => { event.waitUntil(sw.registration.navigationPreload?.enable() || Promise.resolve()); @@ -16,7 +16,7 @@ sw.addEventListener('activate', (event) => { sw.addEventListener('install', () => { sw.skipWaiting(); - sw.caches.open(cacheName).then((cache) => { + caches.open(cacheName).then((cache) => { cache.addAll(assetsToCache); }); }); @@ -29,6 +29,7 @@ sw.addEventListener('push', function (event) { payload = { title: event.data.text() }; } event.waitUntil( + // 需要在 client 请求权限 sw.registration.showNotification(payload.title, { icon: '/logo-144.png', body: payload.body, @@ -44,16 +45,36 @@ sw.addEventListener('notificationclick', function (event) { event.notification.close(); }); +/** + * + * @param {FetchEvent} event + */ +function handleShareTarget(event) { + event.respondWith( + (async () => { + const formData = await event.request.formData(); + const file = formData.get('file'); + // TODO: add to caches -> client open emulator + return Response.redirect('/', 303); + })(), + ); +} + sw.addEventListener('fetch', (event) => { const { request } = event; const requestUrl = new URL(request.url); - const isSomeOrigin = requestUrl.origin === sw.location.origin; + const isSomeOrigin = requestUrl.origin === location.origin; const isApiFetch = (isSomeOrigin && requestUrl.pathname.startsWith('/api')) || requestUrl.origin.includes('api.'); + if (requestUrl.pathname === '/_share_target') { + handleShareTarget(event); + return; + } + if (request.method !== 'GET') return; if (!isSomeOrigin && !isApiFetch) return; - const getCache = () => sw.caches.match(request, { ignoreSearch: request.mode === 'navigate' }); + const getCache = () => caches.match(request, { ignoreSearch: request.mode === 'navigate' }); event.respondWith( (async function () { @@ -64,7 +85,7 @@ sw.addEventListener('fetch', (event) => { .then(async (response) => { if (response.ok) { const responseCache = response.clone(); - sw.caches.open(cacheName).then((cache) => cache.put(request, responseCache)); + caches.open(cacheName).then((cache) => cache.put(request, responseCache)); return response; } else { const cache = await getCache(); diff --git a/packages/webapp/src/drop.ts b/packages/webapp/src/drop.ts index 2a0d872f..2f683dae 100644 --- a/packages/webapp/src/drop.ts +++ b/packages/webapp/src/drop.ts @@ -7,6 +7,7 @@ import { setNesFile } from 'src/configure'; window.launchQueue?.setConsumer(async (launchParams: any) => { if (!launchParams.files.length) return; + // 为啥刷新还是会触发 // https://github.com/WICG/file-system-access/blob/master/EXPLAINER.md#example-code const files = await Promise.all(launchParams.files.map((h: any) => h.getFile())); diff --git a/packages/webapp/src/index.html b/packages/webapp/src/index.html index a5165d72..732575af 100644 --- a/packages/webapp/src/index.html +++ b/packages/webapp/src/index.html @@ -8,7 +8,6 @@ />