-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.js
54 lines (48 loc) · 1.5 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
53
54
const main = document.getElementById('main');
async function fetchTrending() {
const response = await fetch('https://jsonplaceholder.typicode.com/posts');
const data = await response.json();
data.map(function (elem) {
const title = document.createElement('div');
title.innerText = elem.title;
const body = document.createElement('div');
body.innerText = elem.body;
const container = document.createElement('div');
container.className = 'post';
container.append(title, body);
main.append(container);
});
}
window.addEventListener('load', async e => {
if ('serviceWorker' in navigator) {
try {
await navigator.serviceWorker.register('serviceWorker.js');
console.log('SW registered');
} catch (error) {
console.log('SW failed');
}
}
await fetchTrending();
});
let deferredPrompt = null;
window.addEventListener('beforeinstallprompt', function (e) {
console.log('beforeinstallprompt');
// Prevent Chrome 67 and earlier from automatically showing the prompt
e.preventDefault();
// Stash the event so it can be triggered later.
deferredPrompt = e;
});
window.addEventListener('load', function () {
document.querySelector('.center').addEventListener('click', addToHomeScreen);
});
function addToHomeScreen() {
console.log('addToHomeScreen');
if (deferredPrompt) {
deferredPrompt.prompt();
deferredPrompt.userChoice
.then(function (choiceResult) {
console.log(choiceResult.outcome);
deferredPrompt = null;
});
}
}