-
-
Notifications
You must be signed in to change notification settings - Fork 41
/
script.js
57 lines (47 loc) · 1.25 KB
/
script.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
let installEvent = null;
let installButton = document.getElementById("install");
let enableButton = document.getElementById("enable");
enableButton.addEventListener("click", function() {
this.disabled = true;
startPwa(true);
});
if(localStorage["pwa-enabled"]) {
startPwa();
}
function startPwa(firstStart) {
localStorage["pwa-enabled"] = true;
if(firstStart) {
location.reload();
}
window.addEventListener("load", () => {
navigator.serviceWorker.register("/service-worker.js")
.then(registration => {
console.log("Service Worker is registered", registration);
enableButton.parentNode.remove();
})
.catch(err => {
console.error("Registration failed:", err);
});
});
window.addEventListener("beforeinstallprompt", (e) => {
e.preventDefault();
console.log("Ready to install...");
installEvent = e;
document.getElementById("install").style.display = "initial";
});
setTimeout(cacheLinks, 500);
function cacheLinks() {
caches.open("pwa").then(function(cache) {
let linksFound = [];
document.querySelectorAll("a").forEach(function(a) {
linksFound.push(a.href);
});
cache.addAll(linksFound);
});
}
if(installButton) {
installButton.addEventListener("click", function() {
installEvent.prompt();
});
}
}