fix clipboard for the panel

This commit is contained in:
MCHost
2025-06-24 00:04:58 -04:00
parent b7c2fa6d19
commit e3ad40a023
3 changed files with 53 additions and 15 deletions

View File

@ -1,4 +1,3 @@
self.addEventListener('activate', (e) => {
self.clients.claim();
});
@ -6,24 +5,44 @@ self.addEventListener('activate', (e) => {
self.addEventListener('fetch', (e) => {
const reqUrl = e.request.url;
e.respondWith((async() => {
// Open asset cache and see if this request is in it
// Open asset cache
const cache = await caches.open('main');
const match = await caches.match(e.request);
// Request the resource from the network
// Request from network
const netRes = fetch(e.request).then((res) => {
// If the request was successful and this isn't an API call,
// update the cached resource
// Create a new response with updated headers
const newHeaders = new Headers(res.headers);
newHeaders.set('Permissions-Policy', 'clipboard-write=(self), clipboard-read=(self)');
const newRes = new Response(res.body, {
status: res.status,
statusText: res.statusText,
headers: newHeaders
});
// Cache non-API responses
if (res.ok && !reqUrl.match(/\/api\/sftp\/.*$/)) {
cache.put(e.request, res.clone());
cache.put(e.request, newRes.clone());
}
// Return the response
return res;
return newRes;
}).catch(e => {
console.error(e);
return match;
console.error('Fetch error:', e);
if (match) {
// Add header to cached response
const newHeaders = new Headers(match.headers);
newHeaders.set('Permissions-Policy', 'clipboard-write=(self), clipboard-read=(self)');
return new Response(match.body, {
status: match.status,
statusText: match.statusText,
headers: newHeaders
});
}
throw e;
});
// Return the cached resource if it exists
// Otherwise, return the network request
// Return cached or network response
return match || netRes;
})());
});