-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
52 lines (46 loc) · 1.65 KB
/
app.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
// Add this in your index.html connected JavaScript file
// ServiceWorker And user to show the Install App
if ("serviceWorker" in navigator) {
window.addEventListener("load", function () {
navigator.serviceWorker
.register("/serviceWorker.js")
.then(res => {
// console.log("Service worker registered");
if (res.installing) {
// console.log("Service worker installing");
} else if (res.waiting) {
// console.log("Service worker installed");
} else if (res.active) {
// console.log("Service worker active");
}
res.addEventListener("updatefound", () => {
// console.log("New service worker found");
});
})
.catch(err => console.error("Service worker registration failed:", err))
});
let deferredPrompt;
window.addEventListener("beforeinstallprompt", (event) => {
event.preventDefault();
deferredPrompt = event;
});
const installButton = document.getElementById("installButton");
installButton.addEventListener("click", () => {
if (deferredPrompt) {
deferredPrompt.prompt();
deferredPrompt.userChoice
.then((choiceResult) => {
if (choiceResult.outcome === "accepted") {
console.log("User accepted the install prompt");
} else {
console.log("User dismissed the install prompt");
}
deferredPrompt = null;
})
.catch((error) => {
console.error("Error during prompt:", error);
deferredPrompt = null;
});
}
});
}