forked from Offroaders123/Smart-Text-Editor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice-worker.js
118 lines (103 loc) · 3.41 KB
/
service-worker.js
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// @ts-check
/// <reference no-default-lib="true"/>
/// <reference types="better-typescript/worker"/>
const STE = {
version: "Smart Text Editor v4.10.0",
cache: true,
environment: {
get macOSDevice() {
// @ts-expect-error
return (/(macOS|Mac)/i.test(navigator.userAgentData?.platform || navigator.platform) && navigator.standalone === undefined);
}
},
/** @type { File[] } */
shareFiles: []
}
self.addEventListener("activate",event => {
event.waitUntil(caches.keys().then(async keys => {
const results = keys.map(async key => {
if (key.startsWith("Smart Text Editor") && key !== STE.version){
return caches.delete(key);
}
});
await Promise.all(results);
await self.clients.claim();
return messageClients({ action: "service-worker-activated" });
}));
});
self.addEventListener("fetch",async event => {
if (event.request.method === "POST"){
event.waitUntil((async () => {
const formData = await event.request.formData();
const files = formData.getAll("file");
for (const file of files){
STE.shareFiles.push(/** @type { File } */ (file));
}
event.respondWith(Response.redirect("./?share-target=true",303));
})());
return;
}
if (event.request.url === `${(self.location.href.match(/(.*\/).*/) || "")[1]}manifest.webmanifest`){
event.respondWith((async () => {
const cached = await caches.match(event.request);
if (cached !== undefined) return cached;
const fetched = await fetch("./manifest.webmanifest");
const manifest = await fetched.json();
manifest.icons = manifest.icons.filter(/** @param { { platform: string; purpose: string; } } icon */ icon => {
switch (true){
case !STE.environment.macOSDevice && icon.platform !== "macOS":
case STE.environment.macOSDevice && icon.platform === "macOS" || icon.purpose === "maskable": {
return icon;
}
}
});
const result = new Response(JSON.stringify(manifest,null,2),{ headers: { "Content-Type": "text/json" } });
if (STE.cache){
const cache = await caches.open(STE.version);
await cache.put(event.request,result.clone());
}
return result;
})());
return;
}
event.respondWith((async () => {
const cached = await caches.match(event.request);
if (cached !== undefined) return cached;
const fetched = await fetch(event.request);
if (STE.cache){
const cache = await caches.open(STE.version);
await cache.put(event.request,fetched.clone());
}
return fetched;
})());
});
self.addEventListener("message",async event => {
switch (event.data.action){
case "share-target": {
const client = event.source;
client?.postMessage({ action: "share-target", files: STE.shareFiles });
break;
}
case "clear-site-caches": {
const keys = await caches.keys();
const results = keys.map(key => {
if (key.startsWith("Smart Text Editor")){
return caches.delete(key);
}
});
await Promise.all(results);
await messageClients({ action: "clear-site-caches-complete" });
break;
}
}
});
/**
* @param { any } message
* @param { StructuredSerializeOptions } options
*/
async function messageClients(message,options = {}){
const clients = await self.clients.matchAll();
for (const client of clients){
client.postMessage(message,options);
}
}