-
Notifications
You must be signed in to change notification settings - Fork 15
/
pwa.js
executable file
·338 lines (318 loc) · 13.4 KB
/
pwa.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
// V5.1: Option to slice off search URL's.
/*
CUTPOINTS:
Search for these comments and remove everything between them if the feature specified is not required.
//broadcast
A cross-client messaging service. Accepts messages of the type:
{
type:"broadcast"
data:{whatever}
}
Installation is easy! Just follow these three steps:
1. Add this script to your root directory with your index.html.
2. Also add the sample manifest.json at the bottom of this file.
3. Include the following script:
<script>
var pwaManager = new _pwaManager();
</script>
And you're done!
If you want to handle the beforeinstallprompt function, specify it in the options:
var pwaManager = new _pwaManager({
prompt:(e)=>{
e.prompt();
}
});
Call pwaExtract() from a developer console to find a list of active scripts and links on your page for caching.
Place those scripts in the 'urlsToCache' property and you'll be ready to go.
*/
var _pwaManager = (() => {
let serviceWorkerSettings = {
urlsToCache: [
"src/utils.js",
"filemanager.js",
"src/core.js",
"src/core_modules/ui/core.tutorial.js",
"src/core_modules/core/core.docLoading.js",
"src/core_modules/ui/core.dialog.js",
"src/core_modules/ui/core.loadSaveUI.js",
"src/core_modules/core/core.dataUtils.js",
"src/core_modules/core/core.container.js",
"src/core_modules/ui/core.contextMenu.js",
"src/core_modules/core/core.itemfx.js",
"src/core_modules/core/core.operator.js",
"src/core_modules/ui/core.dragdrop.js",
"src/core_modules/core/core.phone.rect.js",
"src/core_modules/core/core.rect.js",
"src/core_modules/ui/core.phone.main.js",
"src/core_modules/ui/core.main.js",
"src/operators/opSelect.js",
"src/genui/dateparser.js",
"src/operators/itemList.searchsort.js",
"src/operators/itemList.js",
"src/operators/itemList.phone.js",
"src/operators/descbox.js",
"src/genui/intervalParser.js",
"src/operators/terminal.js",
"src/operators/inspector.js",
"src/operators/inspectolist.js",
"src/operators/subframe.js",
"src/operators/deltaLogger.js",
"src/3pt/jquery.min.js",
"src/3pt/moment.min.js",
"src/3pt/fullcalendar.min.js",
"src/genui/quickNotify.js",
"src/operators/calendar.js",
"src/3pt/svg.min.js",
"src/3pt/svg.foreignobject.js",
"src/operators/itemcluster.svg.js",
"src/operators/itemcluster.contextmenu.js",
"src/operators/itemcluster.scalegrid.js",
"src/operators/itemcluster.rapidentry.js",
"src/operators/itemcluster.js",
"src/operators/welcome.js",
"src/operators/litem.js",
"src/operators/scriptrunner.js",
"src/operators/timer.js",
"src/saveSources/outputToText.js",
"src/saveSources/localforage2.js",
"src/saveSources/permalink.js",
"src/saveSources/server.js",
"src/saveSources/gitlite.js"
//"manifest.json" DO NOT CACHE THIS OTHERWISE PWA WILL BE NONFUNCTIONAL
],
CACHE_NAME: "version 8x",
SEARCH_SLICE: true,
RETRIEVAL_METHOD: "networkOnly", // cacheReupdate, networkOnly, cacheOnly
debug: false,
forcePassThrough: (url) => {
//replace with your custom function
if (url.includes("lobby")) return true;
if (url.includes("gitload")) return true;
if (url.includes("saveme")) return true;
return false;
}
};
function dbglog(message) {
if (serviceWorkerSettings.debug) console.log(message);
}
function pwaExtract() {
//Extract an array of all the scripts that you might want to cache
let cacheURLs = [];
let elements;
elements = document.querySelectorAll("script");
for (let i = 0; i < elements.length; i++) {
cacheURLs.push(elements[i].src);
}
elements = document.querySelectorAll("link");
for (let i = 0; i < elements.length; i++) {
cacheURLs.push(elements[i].href);
}
return cacheURLs;
}
function _pwaManager(userSettings) {
let me = this;
this.settings = {
serviceWorkerURL: "pwa.js", // This can just be this file! This file serves both functions for the price of one. Yeets!
manifestURL: "manifest.json"
};
Object.assign(this.settings, userSettings);
this.firePrompt = function() {
function tryFirePrompt() {
if (me.deferredPrompt) {
me.deferredPrompt.prompt();
} else {
me.deferredPrompt = "autofire";
}
}
if (document.readyState != "loading") tryFirePrompt();
else document.addEventListener("DOMContentLoaded", tryFirePrompt);
};
//DOM initalisation
this._init = function() {
if ("serviceWorker" in navigator) {
let link = document.createElement("link");
link.rel = "manifest";
link.href = this.settings.manifestURL;
document.head.appendChild(link);
window.addEventListener("load", function() {
navigator.serviceWorker.register(me.settings.serviceWorkerURL).then(
function(registration) {
// Registration was successful
dbglog(
`ServiceWorker registration successful with scope: ${registration.scope}`
);
},
function(err) {
// registration failed :(
dbglog("ServiceWorker registration failed: ", err);
}
);
});
}
};
if (document.readyState != "loading") this._init();
else document.addEventListener("DOMContentLoaded", () => this._init());
//beforeunload event
if (this.settings.prompt) {
window.addEventListener("beforeinstallprompt", this.settings.prompt);
}
}
try {
window.title = window.title;
dbglog("win title ok! I'm a window level.");
} catch (err) {
dbglog("win title not ok! I'm a backend boi.");
//Ok we are a service worker so act like one!!11
self.addEventListener("install", function(event) {
dbglog("install fired.");
self.skipWaiting();
// Perform install steps
event.waitUntil(
caches.open(serviceWorkerSettings.CACHE_NAME).then(function(cache) {
dbglog("Opened cache: " + serviceWorkerSettings.CACHE_NAME);
return cache.addAll(serviceWorkerSettings.urlsToCache).catch((e)=>console.log(e));
})
);
dbglog("hopefully everything is ok... ");
});
let cache;
async function setup() {
dbglog("setup ok");
cache = await caches.open(serviceWorkerSettings.CACHE_NAME);
}
setup();
function updateCache(event) {
return new Promise(async function(resolve) {
setTimeout(async function() {
try {
dbglog(`fetch start: ${event.request}`);
networkResponsePromise = fetch(event.request);
const networkResponse = await networkResponsePromise;
cache.put(event.request, networkResponse.clone());
dbglog("fetch OK: " + event.request.url);
resolve(networkResponse);
} catch (e) {
//network failure
dbglog("fetch error: " + event.request.url);
dbglog(e);
resolve(404);
}
}, 1000); //deliver content to user asap
});
}
dbglog("adding fetch event handler...");
self.addEventListener("fetch", event => {
dbglog("fetch event handler firing...");
switch (serviceWorkerSettings.RETRIEVAL_METHOD) {
case "cacheOnly":
//cache only speed test
if (event.request.method == "GET") {
event.respondWith(caches.match(event.request));
}
break;
case "cacheReupdate":
dbglog("cacheReupdate method.");
//better version with self cache matching
if (event.request.url.startsWith(self.location.origin) && !serviceWorkerSettings.forcePassThrough(event.request.url)) {
if (event.request.method == "GET") {
event.respondWith(
(async function() {
let cachedResponse = undefined;
if (cache) {
cachedResponse = await cache.match(event.request);
if (!cachedResponse && event.request.url.indexOf("?") != -1) {
if (serviceWorkerSettings.SEARCH_SLICE) {
event.request.url = event.request.url.slice(0, event.request.url.indexOf("?"));
cachedResponse = await cache.match(event.request);
} else {
cachedResponse = await cache.match(event.request, {
ignoreSearch: event.request.url.indexOf("?") != -1
});
}
}
}
// Returned the cached response if we have one, otherwise return the network response.
if (event.request.type == "cors") {
return fetch(event.request);
} else {
if (cachedResponse) {
//avoid CORS for things like firebase
dbglog("cacheUpdate: " + event.request.url);
updateCache(event);
return cachedResponse;
} else {
dbglog("passiveUpdate: " + event.request.url);
return updateCache(event);
}
}
})()
);
} else {
dbglog("non-get: " + event.request.url);
event.respondWith(fetch(event.request));
}
} else {
try {
event.respondWith(fetch(event.request));
} catch (e) {
return 404;
}
}
break;
case "networkOnly":
//cache only speed test
try {
event.respondWith(fetch(event.request));
} catch (e) {
// this gets hit a lot
// unlucky, didn't make it
}
break;
}
});
//broadcast
//Credits to here: http://craig-russell.co.uk/2016/01/29/service-worker-messaging.html#.XKKtSVUzZFQ
self.addEventListener("message", function(event) {
if (event.data && event.data.type == "broadcast") {
clients.matchAll().then(clients => {
clients.forEach(client => {
let msgchan = new MessageChannel();
client.postMessage(event.data, [msgchan.port2]);
});
});
}
});
//also this
//https://stackoverflow.com/questions/51562781/service-worker-fetch-event-is-not-firing
self.addEventListener('activate', function(event) {
console.log('Claiming control');
return self.clients.claim();
});
}
return _pwaManager;
})();
/*
A sample web app manifest. Put this in a file in your home directory!
(you may have to add or remove icons - this template has them as a reference.)
{
"short_name": "Webapp",
"name": "My New Webapp",
"icons": [
{
"src": "/images/icons-192.png",
"type": "image/png",
"sizes": "192x192"
},
{
"src": "/images/icons-512.png",
"type": "image/png",
"sizes": "512x512"
}
],
"start_url": "/index.html",
"background_color": "#FFFFFF",
"display": "standalone",
"scope": "/",
"theme_color": "#FFFFFF"
}
*/