-
Notifications
You must be signed in to change notification settings - Fork 0
/
webtexting.js
134 lines (118 loc) · 4.96 KB
/
webtexting.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/**
* webtexting.js: mostly used for subscribing to webpush notifications
*/
// update displayed relative time in all class='timestamp' elements every 5 seconds
function updateTimestamps() {
document.querySelectorAll('.timestamp').forEach((e) => {
e.textContent = moment.utc(e.dataset.timestamp).fromNow();
})
}
$(document).ready(() => {
updateTimestamps();
setInterval(updateTimestamps, 5000);
});
// callback before submitting the new thread dialog, removes any non-numeric characters from the phone number input box
function clean_number() {
document.querySelector("#new-thread-number").value = document.querySelector("#new-thread-number").value.replace(/[^\d+]/g, "");
}
function urlB64ToUint8Array(base64String) {
const padding = '='.repeat((4 - base64String.length % 4) % 4);
const base64 = (base64String + padding)
.replace(/\-/g, '+')
.replace(/_/g, '/');
const rawData = window.atob(base64);
const outputArray = new Uint8Array(rawData.length);
for (let i = 0; i < rawData.length; ++i) {
outputArray[i] = rawData.charCodeAt(i);
}
return outputArray;
}
async function toggleNotifications() {
console.log("toggling notifications for ", window.notification_data);
const registration = await navigator.serviceWorker.ready;
var subscription = await registration.pushManager.getSubscription();
if (!subscription) {
const vapidKeyResponse = await fetch('notifications.php').then(r => r.json());
const convertedVapidKey = urlB64ToUint8Array(vapidKeyResponse.vapid_key);
// this displays the notification permission prompt to the user
try {
subscription = await registration.pushManager.subscribe({ applicationServerKey: convertedVapidKey,userVisibleOnly: true });
} catch (e) {
console.log("error subscribing: ", e);
//document.querySelector('#notification-btn').style.display = 'none';
return;
}
}
const currentState = document.querySelector('#notification-btn').dataset.state;
const toggleResponse = await fetch('notifications.php', {
method: "POST",
body: JSON.stringify({
endpoint: subscription.toJSON(),
//if either is "on" state is true we need to turn off notifications
state: (currentState == "on" || currentState == "all") ? "off" : "on",
extension_uuid: window.notification_data.extension_uuid,
// remote_identifier: window.notification_data.remote_identifier,
}),
}).then((r) => r.json());
setNotificationButtonState(toggleResponse);
}
function setNotificationButtonState(notificationState) {
console.log("notificationState:", notificationState.state);
const btn = document.querySelector('#notification-btn');
if(!btn) {
return;
}
btn.dataset.state = notificationState.state;
//const icon = btn.querySelector('.fas')
switch(notificationState.state) {
case "all":
if(window.notification_data.remote_identifier) {
//btn.style.display = 'none';
btn.classList.add("fa-bell");
btn.classList.remove("fa-bell-slash");
return;
}
// note no "break", execution continues to "on" case
case "on":
btn.classList.add("fa-bell");
btn.classList.remove("fa-bell-slash");
break;
case "off":
btn.classList.add("fa-bell-slash");
btn.classList.remove("fa-bell");
break;
}
const label = btn.querySelector('.button-label');
if(label) {
label.textContent = notificationState.state;
}
}
if (window.notification_data) {
navigator.serviceWorker.register('webtexting-worker.js').then(async function (registration) {
const state = await registration.pushManager.permissionState({userVisibleOnly:true});
console.log("push notification permission state:", state);
if (state == "denied") {
console.log("Push notification permissions denied. Please contact [email protected].")
return;
}
var subscription = await registration.pushManager.getSubscription();
console.log("permissionState = " , subscription)
var buttonState = {state: "off"};
if(subscription) {
console.log(subscription.toJSON());
buttonState = await fetch('notifications.php', {
method: "POST",
body: JSON.stringify({
endpoint: subscription.toJSON(),
extension_uuid: window.notification_data.extension_uuid,
// remote_identifier: window.notification_data.remote_identifier,
}),
}).then((r) => r.json());
}
let btn = document.querySelector('#notification-btn');
if(btn) {
btn.style.display = 'inline-block';
setNotificationButtonState(buttonState);
}
});
}