-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpwa.js
More file actions
51 lines (46 loc) · 1.82 KB
/
Copy pathpwa.js
File metadata and controls
51 lines (46 loc) · 1.82 KB
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
(function () {
var installButton = document.getElementById('install');
var deferredPrompt = null;
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('./sw.js', {scope: './'}).catch(console.error);
// Hand the worker what this page already loaded (see sw.js 'message').
// Twice: once when it is ready, and again after the app has had time to
// pull its packages.
var sendLoaded = function () {
navigator.serviceWorker.ready.then(function (registration) {
if (!registration.active) return;
var urls = performance.getEntriesByType('resource').map(function (entry) {
return entry.name;
}).filter(function (url) {
return url.indexOf('http') === 0;
});
urls.push(location.href);
registration.active.postMessage({type: 'cache-urls', urls: urls});
});
};
sendLoaded();
window.addEventListener('load', function () { setTimeout(sendLoaded, 15000); });
}
window.addEventListener('beforeinstallprompt', function (event) {
event.preventDefault();
deferredPrompt = event;
installButton.hidden = false;
});
installButton.addEventListener('click', function () {
if (!deferredPrompt) return;
deferredPrompt.prompt();
deferredPrompt.userChoice.finally(function () {
deferredPrompt = null;
installButton.hidden = true;
});
});
// main.py may say what it is doing; if it does not (an example dropped in
// as main.py), do not leave "Loading Python…" up once the script has run.
window.addEventListener('py:all-done', function () {
var status = document.getElementById('status');
if (status && status.textContent === 'Loading Python…') status.textContent = 'Running.';
});
window.addEventListener('appinstalled', function () {
installButton.hidden = true;
});
})();