-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathindex.html
316 lines (280 loc) · 8.36 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
<!DOCTYPE html>
<html>
<head>
<title>Hyperbeam timeout example ⏰</title>
<style>
* {
--primary: #464d77;
--primary-dark: #3d4453;
--primary-darker: #2d3142;
--primary-10-percent: rgba(70, 77, 119, 0.1);
--primary-20-percent: rgba(70, 77, 119, 0.2);
--secondary: #36827f;
}
body {
font-family: Arial, sans-serif;
margin: 0;
}
button {
border: none;
border-radius: 4px;
background: #eee;
background-color: var(--primary);
padding: 8px 16px;
color: #fff;
font-size: 1em;
cursor: pointer;
}
button:hover {
background-color: var(--primary-dark);
}
button:active {
background-color: var(--primary-darker);
}
.modal {
position: fixed;
z-index: 1;
left: 0;
top: 0;
width: 100%;
height: 100%;
overflow: auto;
background-color: rgb(0, 0, 0);
background-color: rgba(0, 0, 0, 0.4);
}
.modal-content {
background-color: #fefefe;
margin: 15% auto;
padding: 20px;
border: 1px solid #888;
width: 500px;
border-radius: 8px;
}
.modal-content h1 {
margin-top: 0;
}
.modal-content button + button.closeModal {
color: var(--primary);
background-color: transparent;
border: 1px solid var(--primary);
}
.modal-content button + button.closeModal:hover {
background-color: var(--primary-10-percent);
}
.modal-content button + button.closeModal:active {
background-color: var(--primary-20-percent);
}
.close {
color: #aaa;
float: right;
font-size: 28px;
font-weight: bold;
}
.close:hover,
.close:focus {
color: black;
text-decoration: none;
cursor: pointer;
}
.timeout {
font-weight: bold;
}
.managePremium {
background-color: var(--primary);
color: #fff;
text-align: center;
padding: 8px 0;
}
.managePremium button {
border: 1px solid #fff;
background-color: transparent;
color: #fff;
margin: 0 8px;
padding: 4px 8px;
}
.managePremium button:hover {
background-color: rgba(255, 255, 255, 0.1);
}
.managePremium button:active {
background-color: rgba(255, 255, 255, 0.2);
}
#restart {
background-color: var(--secondary);
}
.hidden {
display: none;
}
#hbContainer {
height: calc(100vh - 45px); /* 45px is the height of the banner */
width: auto;
}
#restart:not(.hidden) ~ #hbContainer {
height: calc(100vh - 90px); /* 90px is the height of two banners */
}
</style>
</head>
<body>
<div class="managePremium hidden" id="upsell">
Tired of timeouts? <button class="upgradeBtn">Upgrade</button>
</div>
<div class="managePremium hidden" id="cancel">
Upgraded to premium <button class="cancelBtn">Cancel</button>
</div>
<div class="managePremium hidden" id="restart">
<span id="closeReason"></span><button id="restartBtn">Restart</button>
</div>
<div id="absoluteTimeoutModal" class="modal hidden">
<div class="modal-content">
<span class="close closeModal">×</span>
<h1>Level up your experience</h1>
<p>
Your free co-browser will time out in <span class="timeout"></span>.
</p>
<p>Tired of timeouts? Subscribe to get unlimited co-browsing.</p>
<button class="upgradeBtn">Upgrade</button>
<button class="closeModal">Maybe later</button>
</div>
</div>
<div id="inactiveTimeoutModal" class="modal hidden">
<div class="modal-content">
<span class="close closeModal">×</span>
<h1>Are you still there?</h1>
<p>
Your session will close in <span class="timeout"></span> due to
inactivity.
</p>
<button class="closeModal">I'm still here!</button>
</div>
</div>
<div id="hbContainer"></div>
<script type="module">
import Hyperbeam from "https://unpkg.com/@hyperbeam/web@latest/dist/index.js";
let hb;
useHyperbeam();
addDOMHandlers();
async function useHyperbeam() {
const resp = await fetch("/computer");
const { hasPremium, computer } = await resp.json();
restart.classList.add("hidden");
setPremium(hasPremium);
hb = await Hyperbeam(hbContainer, computer.embed_url, {
onCloseWarning(e) {
if (e.deadline) {
showCloseWarning(e.type, e.deadline.delay);
} else {
hideCloseWarning(e.type);
}
},
onDisconnect(e) {
showDisconnect(e.type);
hb.destroy();
hb = undefined;
},
});
}
let interval;
function beginCountdown(delay) {
if (interval) {
clearInterval(interval);
}
let seconds = Math.floor(delay / 1000);
const timeouts = document.getElementsByClassName("timeout");
const updateTimeouts = () => {
for (const timeout of timeouts) {
timeout.innerText = seconds.toString() + "s";
}
};
updateTimeouts();
interval = setInterval(() => {
seconds -= 1;
updateTimeouts();
}, 1000);
}
function showCloseWarning(type, delay) {
switch (type) {
case "inactive":
beginCountdown(delay);
inactiveTimeoutModal.classList.remove("hidden");
break;
case "absolute":
beginCountdown(delay);
absoluteTimeoutModal.classList.remove("hidden");
break;
}
}
function hideCloseWarning(type) {
switch (type) {
case "inactive":
inactiveTimeoutModal.classList.add("hidden");
break;
case "absolute":
absoluteTimeoutModal.classList.add("hidden");
break;
}
}
function showDisconnect(type) {
const modals = document.getElementsByClassName("modal");
for (const modal of modals) {
modal.classList.add("hidden");
}
restart.classList.remove("hidden");
switch (type) {
case "inactive":
closeReason.innerText = "Co-browser shut down due to inactivity ";
break;
case "absolute":
closeReason.innerText = "Co-browser free time exceeded ";
break;
}
}
function setPremium(hasPremium) {
if (hasPremium) {
upsell.classList.add("hidden");
cancel.classList.remove("hidden");
} else {
cancel.classList.add("hidden");
upsell.classList.remove("hidden");
}
}
async function onUpgrade(e) {
e.target.disabled = true;
await fetch("/upgrade", { method: "POST" });
setPremium(true);
e.target.disabled = false;
}
async function onCancel(e) {
e.target.disabled = true;
await fetch("/cancel", { method: "POST" });
setPremium(false);
e.target.disabled = false;
}
function onCloseModal(modal) {
modal.classList.add("hidden");
if (hb) {
hb.ping();
}
}
function addDOMHandlers() {
const modals = document.getElementsByClassName("modal");
const upgradeBtns = document.getElementsByClassName("upgradeBtn");
const cancelBtns = document.getElementsByClassName("cancelBtn");
restartBtn.addEventListener("click", useHyperbeam);
for (const upgradeBtn of upgradeBtns) {
upgradeBtn.addEventListener("click", onUpgrade);
}
for (const cancelBtn of cancelBtns) {
cancelBtn.addEventListener("click", onCancel);
}
for (const modal of modals) {
const close = modal.getElementsByClassName("close")[0];
const buttons = modal.getElementsByTagName("button");
for (const closer of [close, ...buttons]) {
closer.addEventListener("click", () => {
onCloseModal(modal);
});
}
}
}
</script>
</body>
</html>