-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
54 lines (49 loc) · 1.89 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
const divInstall = document.getElementById("installContainer");
const butInstall = document.getElementById("butInstall");
/* Put code here */
window.addEventListener("beforeinstallprompt", event => {
console.log("👍", "beforeinstallprompt", event);
// Stash the event so it can be triggered later.
window.deferredPrompt = event;
// Remove the 'hidden' class from the install button container
divInstall.classList.toggle("hidden", false);
});
butInstall.addEventListener("click", async () => {
console.log("👍", "butInstall-clicked");
const promptEvent = window.deferredPrompt;
if (!promptEvent) {
// The deferred prompt isn't available.
return;
}
// Show the install prompt.
promptEvent.prompt();
// Log the result
const result = await promptEvent.userChoice;
console.log("👍", "userChoice", result);
// Reset the deferred prompt variable, since
// prompt() can only be called once.
window.deferredPrompt = null;
// Hide the install button.
divInstall.classList.toggle("hidden", true);
});
window.addEventListener("appinstalled", event => {
console.log("👍", "appinstalled", event);
// Clear the deferredPrompt so it can be garbage collected
window.deferredPrompt = null;
});
/* Only register a service worker if it's supported */
if ("serviceWorker" in navigator) {
navigator.serviceWorker.register("service-worker.js");
}
/**
* Warn the page must be served over HTTPS
* The `beforeinstallprompt` event won't fire if the page is served over HTTP.
* Installability requires a service worker with a fetch event handler, and
* if the page isn't served over HTTPS, the service worker won't load.
*/
if (window.location.protocol === "http:") {
const requireHTTPS = document.getElementById("requireHTTPS");
const link = requireHTTPS.querySelector("a");
link.href = window.location.href.replace("http://", "https://");
requireHTTPS.classList.remove("hidden");
}