2023-04-20 22:04:57 +02:00
|
|
|
const cacheVersion = 'v1.6.2';
|
2023-01-23 20:40:08 +01:00
|
|
|
const cacheTitle = `pairdrop-cache-${cacheVersion}`;
|
2023-01-23 04:51:22 +01:00
|
|
|
const urlsToCache = [
|
|
|
|
'index.html',
|
|
|
|
'./',
|
|
|
|
'styles.css',
|
|
|
|
'scripts/network.js',
|
|
|
|
'scripts/ui.js',
|
|
|
|
'scripts/util.js',
|
|
|
|
'scripts/qrcode.js',
|
|
|
|
'scripts/zip.min.js',
|
|
|
|
'scripts/NoSleep.min.js',
|
|
|
|
'scripts/theme.js',
|
|
|
|
'sounds/blop.mp3',
|
|
|
|
'images/favicon-96x96.png',
|
|
|
|
'images/favicon-96x96-notification.png',
|
|
|
|
'images/android-chrome-192x192.png',
|
|
|
|
'images/android-chrome-192x192-maskable.png',
|
|
|
|
'images/android-chrome-512x512.png',
|
|
|
|
'images/android-chrome-512x512-maskable.png',
|
|
|
|
'images/apple-touch-icon.png',
|
2018-10-24 17:43:50 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
self.addEventListener('install', function(event) {
|
|
|
|
// Perform install steps
|
2023-01-23 00:03:26 +01:00
|
|
|
event.waitUntil(
|
2023-01-23 20:40:08 +01:00
|
|
|
caches.open(cacheTitle)
|
2023-01-23 00:03:26 +01:00
|
|
|
.then(function(cache) {
|
2023-01-23 20:40:08 +01:00
|
|
|
return cache.addAll(urlsToCache).then(_ => {
|
|
|
|
console.log('All files cached.');
|
|
|
|
});
|
|
|
|
})
|
2023-01-23 00:03:26 +01:00
|
|
|
);
|
2018-10-24 17:43:50 +02:00
|
|
|
});
|
|
|
|
|
2023-01-23 20:40:08 +01:00
|
|
|
// fetch the resource from the network
|
|
|
|
const fromNetwork = (request, timeout) =>
|
|
|
|
new Promise((fulfill, reject) => {
|
|
|
|
const timeoutId = setTimeout(reject, timeout);
|
|
|
|
fetch(request).then(response => {
|
|
|
|
clearTimeout(timeoutId);
|
|
|
|
fulfill(response);
|
|
|
|
update(request);
|
|
|
|
}, reject);
|
|
|
|
});
|
|
|
|
|
|
|
|
// fetch the resource from the browser cache
|
|
|
|
const fromCache = request =>
|
|
|
|
caches
|
|
|
|
.open(cacheTitle)
|
|
|
|
.then(cache =>
|
|
|
|
cache
|
|
|
|
.match(request)
|
|
|
|
.then(matching => matching || cache.match('/offline/'))
|
|
|
|
);
|
|
|
|
|
|
|
|
// cache the current page to make it available for offline
|
|
|
|
const update = request =>
|
|
|
|
caches
|
|
|
|
.open(cacheTitle)
|
|
|
|
.then(cache =>
|
|
|
|
fetch(request).then(response => {
|
|
|
|
cache.put(request, response).then(_ => {
|
|
|
|
console.log("Page successfully cached.")
|
|
|
|
})
|
|
|
|
})
|
|
|
|
);
|
2018-10-24 17:43:50 +02:00
|
|
|
|
2023-01-23 20:40:08 +01:00
|
|
|
// general strategy when making a request (eg if online try to fetch it
|
|
|
|
// from the network with a timeout, if something fails serve from cache)
|
2018-10-24 17:43:50 +02:00
|
|
|
self.addEventListener('fetch', function(event) {
|
2023-01-18 21:01:29 +01:00
|
|
|
if (event.request.method === "POST") {
|
|
|
|
// Requests related to Web Share Target.
|
2023-03-27 21:49:33 +02:00
|
|
|
event.respondWith((async () => {
|
|
|
|
let share_url = await evaluateRequestData(event.request);
|
2023-03-28 19:07:33 +02:00
|
|
|
share_url = event.request.url + share_url;
|
2023-03-27 21:49:33 +02:00
|
|
|
return Response.redirect(encodeURI(share_url), 302);
|
|
|
|
})());
|
2023-01-18 21:01:29 +01:00
|
|
|
} else {
|
|
|
|
// Regular requests not related to Web Share Target.
|
|
|
|
event.respondWith(
|
2023-01-23 20:40:08 +01:00
|
|
|
fromNetwork(event.request, 10000).catch(() => fromCache(event.request))
|
2023-01-18 21:01:29 +01:00
|
|
|
);
|
2023-01-23 20:40:08 +01:00
|
|
|
event.waitUntil(update(event.request));
|
2023-01-18 21:01:29 +01:00
|
|
|
}
|
2018-10-24 17:43:50 +02:00
|
|
|
});
|
2020-05-19 22:52:27 +02:00
|
|
|
|
|
|
|
|
2023-01-23 20:40:08 +01:00
|
|
|
// on activation, we clean up the previously registered service workers
|
|
|
|
self.addEventListener('activate', evt =>
|
|
|
|
evt.waitUntil(
|
|
|
|
caches.keys().then(cacheNames => {
|
|
|
|
return Promise.all(
|
|
|
|
cacheNames.map(cacheName => {
|
|
|
|
if (cacheName !== cacheTitle) {
|
|
|
|
return caches.delete(cacheName);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
);
|
2020-05-19 22:52:27 +02:00
|
|
|
})
|
2023-01-23 20:40:08 +01:00
|
|
|
)
|
|
|
|
);
|
2023-03-09 17:03:44 +01:00
|
|
|
|
|
|
|
const evaluateRequestData = async function (request) {
|
|
|
|
const formData = await request.formData();
|
|
|
|
const title = formData.get("title");
|
|
|
|
const text = formData.get("text");
|
|
|
|
const url = formData.get("url");
|
2023-03-27 21:49:33 +02:00
|
|
|
const files = formData.getAll("allfiles");
|
2023-03-09 17:03:44 +01:00
|
|
|
|
2023-03-28 19:07:33 +02:00
|
|
|
|
|
|
|
return new Promise(async (resolve) => {
|
|
|
|
if (files && files.length > 0) {
|
|
|
|
let fileObjects = [];
|
|
|
|
for (let i=0; i<files.length; i++) {
|
|
|
|
fileObjects.push({
|
|
|
|
name: files[i].name,
|
|
|
|
buffer: await files[i].arrayBuffer()
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-03-09 17:03:44 +01:00
|
|
|
const DBOpenRequest = indexedDB.open('pairdrop_store');
|
2023-03-28 19:07:33 +02:00
|
|
|
DBOpenRequest.onsuccess = e => {
|
2023-03-09 17:03:44 +01:00
|
|
|
const db = e.target.result;
|
|
|
|
for (let i = 0; i < fileObjects.length; i++) {
|
|
|
|
const transaction = db.transaction('share_target_files', 'readwrite');
|
|
|
|
const objectStore = transaction.objectStore('share_target_files');
|
|
|
|
|
|
|
|
const objectStoreRequest = objectStore.add(fileObjects[i]);
|
|
|
|
objectStoreRequest.onsuccess = _ => {
|
2023-03-28 19:07:33 +02:00
|
|
|
if (i === fileObjects.length - 1) resolve('?share-target=files');
|
2023-03-09 17:03:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
DBOpenRequest.onerror = _ => {
|
2023-03-28 19:07:33 +02:00
|
|
|
resolve('');
|
2023-03-09 17:03:44 +01:00
|
|
|
}
|
|
|
|
} else {
|
2023-03-28 19:07:33 +02:00
|
|
|
let share_url = '?share-target=text';
|
|
|
|
|
|
|
|
if (title) share_url += `&title=${title}`;
|
|
|
|
if (text) share_url += `&text=${text}`;
|
|
|
|
if (url) share_url += `&url=${url}`;
|
|
|
|
|
|
|
|
resolve(share_url);
|
2023-03-09 17:03:44 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|