-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
86 lines (75 loc) · 2.19 KB
/
main.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
const first = document.querySelector('#number1');
const second = document.querySelector('#number2');
const result = document.querySelector('.result');
async function getSW() {
return navigator.serviceWorker.getRegistration('/worker.js');
}
let a = document.getElementById("audio");
if (navigator.serviceWorker) {
let y = navigator.serviceWorker;
navigator.serviceWorker.register('/worker.js').then(function(registration) {
console.log('Reg succ:', registration);
navigator.serviceWorker.controller.postMessage(2);
}, function(error) {
console.log('Reg fail:', error);
});
navigator.serviceWorker.onmessage = async function(event) {
dat = event.data;
console.log("Hey got message");
let sw = await getSW();
a.pause();
a.currentTime = 0;
let nots = await sw.getNotifications();
nots.forEach(not => {
not.close();
});
/*
sw.showNotification('test', dat).then(res => {
notification = res
});
*/
a.play();
}
} else {
console.log('Your browser doesn\'t support web workers.')
}
first.onchange = function() {
let a = document.getElementById("audio");
a.play();
}
function askNotificationPermission() {
// function to actually ask the permissions
function handlePermission(permission) {
// set the button to shown or hidden, depending on what the user answers
if(Notification.permission === 'denied' || Notification.permission === 'default') {
notificationBtn.style.display = 'block';
} else {
notificationBtn.style.display = 'none';
}
}
// Let's check if the browser supports notifications
if (!('Notification' in window)) {
console.log("This browser does not support notifications.");
} else {
if(checkNotificationPromise()) {
Notification.requestPermission()
.then((permission) => {
handlePermission(permission);
})
} else {
Notification.requestPermission(function(permission) {
handlePermission(permission);
});
}
}
}
function checkNotificationPromise() {
try {
Notification.requestPermission().then();
} catch(e) {
return false;
}
return true;
}
let notificationBtn = document.getElementById("enable");
notificationBtn.onclick = askNotificationPermission;