-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
61 lines (52 loc) · 2.11 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
import { checkAll } from "./js/checker.js";
const publickey = "BLA0fdKCDGwXsUXeNNery52icIYEaPlDXVD-qkqRCSmxzObJH_Wt1ge2ZXuCE5GZAzIGMwNo1eOhLYcTdveNHng";
document.getElementById('enabler').addEventListener('click', async () => {
if (!('serviceWorker' in navigator)) {
console.error('Service Worker not supported in this browser.');
return;
}
try {
const registration = await navigator.serviceWorker.register('./service-worker.js');
const permission = await Notification.requestPermission();
if (permission !== 'granted') {
console.error('Permission not granted for notifications.');
return;
}
const subscription = await registration.pushManager.subscribe({
userVisibleOnly: true,
applicationServerKey: urlBase64ToUint8Array(publickey)
});
// TODO : change to real server when done
await fetch('https://nathan-pc.taile828dd.ts.net/register', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(subscription),
});
} catch (error) {
console.error(error);
}
checkAll();
});
// document.getElementById('enabler').addEventListener('click', async () => {
// const registration = await navigator.serviceWorker.ready;
// if (registration.active) {
// registration.active.postMessage({
// type: 'simulate-push',
// payload: {
// title: 'THREAT DETECTED',
// body: 'Threat at \"Lakeforest Mall\"',
// icon: 'https://www.soteria-security.us/assets/images/logo.svg'
// }
// });
// } else {
// console.error('No active Service Worker found.');
// }
// });
function urlBase64ToUint8Array(base64String) {
const padding = '='.repeat((4 - base64String.length % 4) % 4);
const base64 = (base64String + padding).replace(/-/g, '+').replace(/_/g, '/');
const rawData = window.atob(base64);
return Uint8Array.from([...rawData].map((char) => char.charCodeAt(0)));
}