-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
50 lines (42 loc) · 1.54 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
const imgElem = document.querySelector("img");
function randomValueFromArray(array) {
const randomNo = Math.floor(Math.random() * array.length);
return array[randomNo];
}
setInterval(() => {
const randomChoice = randomValueFromArray(images);
imgElem.src = `images/${randomChoice}.jpg`;
}, 2000);
// Register service worker to control making site work offline
if ("serviceWorker" in navigator) {
navigator.serviceWorker.register("/pwa-examples/a2hs/sw.js").then(() => {
console.log("Service Worker Registered");
});
}
// Code to handle install prompt on desktop
let deferredPrompt;
const addBtn = document.querySelector(".add-button");
addBtn.style.display = "none";
window.addEventListener("beforeinstallprompt", (e) => {
// Prevent Chrome 67 and earlier from automatically showing the prompt
e.preventDefault();
// Stash the event so it can be triggered later.
deferredPrompt = e;
// Update UI to notify the user they can add to home screen
addBtn.style.display = "block";
addBtn.addEventListener("click", () => {
// hide our user interface that shows our A2HS button
addBtn.style.display = "none";
// Show the prompt
deferredPrompt.prompt();
// Wait for the user to respond to the prompt
deferredPrompt.userChoice.then((choiceResult) => {
if (choiceResult.outcome === "accepted") {
console.log("User accepted the A2HS prompt");
} else {
console.log("User dismissed the A2HS prompt");
}
deferredPrompt = null;
});
});
});