-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
62 lines (52 loc) · 1.77 KB
/
index.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
58
59
60
61
62
var vmc = {
makeApiRequest: function(apiKey) {
var url = 'https://admin.masha24.by/api/?code=' + apiKey;
fetch(url).then(function(response) {
return response.json();
}).then(function(json) {
var titles = json.items.map(function(item) {
return '"' + item.snippet.title + '"';
}).join(', ');
console.log(titles);
});
}
}
// Register service worker to control making site work offline
if ('serviceWorker' in navigator) {
navigator.serviceWorker
.register('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';
document.addEventListener('DOMContentLoaded', (event) => {
const parsedUrl = new URL(window.location.href);
if (parsedUrl.searchParams.get("code")) {
vmc.makeApiRequest(parsedUrl.searchParams.get("code"))
}
});
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;
});
});
});