-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
149 lines (127 loc) · 4.66 KB
/
index.html
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<!DOCTYPE html>
<html class="mdc-typography">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>Zeus Flight Deals</title>
<link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons" />
<link rel="stylesheet" href="https://code.getmdl.io/1.3.0/material.teal-indigo.min.css" />
<style>
.zeus-align-center {
justify-content: center;
}
.zeus-logo {
color: #ff5;
display: block;
font-size: 80px;
padding: 20px 0 0;
text-shadow: 0 3px 5px #bbb;
}
</style>
</head>
<body>
<div class="mdl-grid zeus-align-center">
<div class="mdl-card mdl-shadow--2dp mdl-cell mdl-cell--middle mdl-cell--4-col mdl-typography--text-center">
<i class="material-icons zeus-logo">flash_on</i>
<div class="mdl-card__title mdl-card--expand zeus-align-center">
<h2 class="mdl-card__title-text">Zeus</h2>
</div>
<div class="mdl-card__supporting-text">
<p>Curated international flight deals sent immediately to your devices.</p>
</div>
<div class="mdl-card__actions mdl-card--border">
<button class="mdl-button mdl-js-button mdl-button--raised mdl-button--accent mdl-js-ripple-effect zeus-action--add-notifications" disabled>Loading</button>
</div>
</div>
</div>
<p class="mdl-typography--text-center"><a href="https://docs.google.com/forms/d/18PsuepzGdCdzjQV2GHbVdSlOesv8e0qQlonmtTmad2k">Send feedback</a></p>
<dialog class="mdl-dialog zeus-dialog--unsupported">
<h4 class="mdl-dialog__title">We are sorry</h4>
<div class="mdl-dialog__content">
<p>It seems currently we do not support your browser.</p>
</div>
<div class="mdl-dialog__actions">
<button type="button" class="mdl-button">OK</button>
</div>
</dialog>
<script defer src="https://code.getmdl.io/1.3.0/material.min.js"></script>
<script>
const
button = document.querySelector('.zeus-action--add-notifications'),
dialog = document.querySelector('.zeus-dialog--unsupported');
let swRegistration, isSubscribed;
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;
}
const checkSubscription = subscription => {
isSubscribed = subscription && subscription.endpoint && !subscription.unsubscribed;
// Update UI
button.disabled = false;
button.innerHTML = isSubscribed ? 'Unsubscribe me' : 'Subscribe me';
};
const onSubscription = subscription => {
checkSubscription(subscription);
fetch('/subscribe', {
method: 'POST',
body: JSON.stringify({ subscription, isSubscribed }),
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
}
}).then(res => res.json())
.then(console.log.bind(console));
};
const onError = error => console.error('Service Worker Error', error);
const notSupported = () => button.innerHTML = 'Unsupported browser';
if ('serviceWorker' in navigator && 'PushManager' in window) {
navigator.serviceWorker.register('sw.js')
.then(swreg => {
swRegistration = swreg;
swRegistration.pushManager.getSubscription()
.then(checkSubscription);
})
.catch(onError);
} else {
notSupported();
}
window.addEventListener('load', () => {
button.addEventListener('click', () => {
try {
const applicationServerKey = urlB64ToUint8Array('BJP3ExpuLdivzELBkycImD5tvc3LOuS3zgp2kq9XPUgw0vOzkTp0XHkNbhdxAMCqnKfVweyg1yVNxdM8yfpYvFY');
if (isSubscribed) {
swRegistration.pushManager.getSubscription()
.then(subscription => {
if (subscription.unsubscribe()) {
subscription.unsubscribed = true;
}
return subscription;
})
.catch(onError)
.then(onSubscription);
return;
}
swRegistration.pushManager.subscribe({
userVisibleOnly: true,
applicationServerKey: applicationServerKey
})
.then(onSubscription)
.catch(onError);
} catch (e) {
dialog.showModal();
notSupported();
}
}, false);
dialog.querySelector('button').addEventListener('click', () => dialog.close());
}, false);
</script>
</body>
</html>