-
Notifications
You must be signed in to change notification settings - Fork 0
/
send.html
290 lines (289 loc) · 13.1 KB
/
send.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
<!DOCTYPE html>
<html lang="en" data-location="local" data-theme="system">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="Capture tasks without revealing your inbox">
<title>Send to Frogtab</title>
<link rel="icon" href="favicons/icon-16.png" sizes="16x16" type="image/png">
<link rel="icon" href="favicons/icon-32.png" sizes="32x32" type="image/png">
<link rel="apple-touch-icon" sizes="180x180" href="favicons/icon-180-apple.png">
<link rel="manifest" href="manifest.webmanifest">
<link rel="stylesheet" href="style.css?sha1=b3f3f8236df9321ee6441e2ff5870af57e0a19c9">
</head>
<body>
<div class="container restricted">
<div class="interface">
<div id="popup-connecting" class="popup">
<svg role="img" aria-label="Cloud connecting icon" width="20px" height="20px" stroke-width="1.5" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg" color="currentColor"><path d="M20 17.607c1.494-.585 3-1.918 3-4.607 0-4-3.333-5-5-5 0-2 0-6-6-6S6 6 6 8c-1.667 0-5 1-5 5 0 2.689 1.506 4.022 3 4.607M7.58 19.487l1.768 1.768a4 4 0 005.657 0l.354-.353" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"></path><path d="M7.934 21.962l-.353-2.475 2.474.354-2.12 2.121zM16.298 16.902l-1.768-1.768a4 4 0 00-5.657 0l-.353.353" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"></path><path d="M15.944 14.427l.354 2.475-2.475-.354 2.121-2.121z" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"></path></svg>Connecting…
</div>
<div id="popup-connected" class="popup">
<svg role="img" aria-label="Cloud check icon" width="20px" height="20px" stroke-width="1.5" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg" color="currentColor"><path d="M8 18l3 3 5-5" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"></path><path d="M20 17.607c1.494-.585 3-1.918 3-4.607 0-4-3.333-5-5-5 0-2 0-6-6-6S6 6 6 8c-1.667 0-5 1-5 5 0 2.689 1.506 4.022 3 4.607" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"></path></svg>Connection is end-to-end encrypted
</div>
<div id="popup-disconnected" class="popup">
<svg role="img" aria-label="Cloud error icon" width="20px" height="20px" stroke-width="1.5" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg" color="currentColor"><path d="M9 22l3-3m3-3l-3 3m0 0l-3-3m3 3l3 3M20 17.607c1.494-.585 3-1.918 3-4.607 0-4-3.333-5-5-5 0-2 0-6-6-6S6 6 6 8c-1.667 0-5 1-5 5 0 2.689 1.506 4.022 3 4.607" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"></path></svg>Not connected
</div>
<div id="popup-sending" class="popup">
<svg role="img" aria-label="Cloud sending icon" width="20px" height="20px" stroke-width="1.5" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg" color="currentColor"><path d="M12 22v-9m0 0l3.5 3.5M12 13l-3.5 3.5M20 17.607c1.494-.585 3-1.918 3-4.607 0-4-3.333-5-5-5 0-2 0-6-6-6S6 6 6 8c-1.667 0-5 1-5 5 0 2.689 1.506 4.022 3 4.607" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"></path></svg>Connection is end-to-end encrypted
</div>
<div id="popup-failure" class="popup">
<svg role="img" aria-label="Warning icon" width="20px" height="20px" stroke-width="1.5" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg" color="currentColor"><path d="M20.043 21H3.957c-1.538 0-2.5-1.664-1.734-2.997l8.043-13.988c.77-1.337 2.699-1.337 3.468 0l8.043 13.988C22.543 19.336 21.58 21 20.043 21zM12 9v4" stroke="currentColor" stroke-width="1.5" stroke-linecap="round"></path><path d="M12 17.01l.01-.011" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"></path></svg>Failed to send. Please try again
</div>
<div class="main">
<textarea tabindex="0" id="editor" class="display" placeholder="Send to your inbox…"></textarea>
</div>
<div class="actions">
<div class="action primary">
<span tabindex="-1" id="send" class="circle-button" title="Send"><svg role="img" aria-label="Send icon" width="24px" height="24px" viewBox="0 0 24 24" stroke-width="1.5" fill="none" xmlns="http://www.w3.org/2000/svg" color="currentColor"><path d="M22 12L3 20l3.563-8L3 4l19 8zM6.5 12H22" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"></path></svg></span>
</div>
</div>
</div>
</div>
<script>
function delayForUX(f) {
return Promise.all([
f(),
new Promise(resolve => window.setTimeout(resolve, 1500))
]);
}
function tryToSendLocal() {
const valueToSend = dom.editor.value.trim();
if (valueToSend == "") {
return;
}
dom.editor.value = "";
dom.editor.focus();
dom.send.classList.add("long-hover");
window.setTimeout(() => {
dom.send.classList.remove("enabled");
dom.send.classList.remove("long-hover");
dom.send.tabIndex = -1;
}, 300);
if (localStorage.getItem("value.inbox") === null) {
localStorage.setItem("value.inbox", "");
}
let valueInbox = localStorage.getItem("value.inbox").trimStart();
if (valueInbox != "") {
valueInbox = `\n\n${valueInbox}`;
}
valueInbox = `${valueToSend}${valueInbox}`;
localStorage.setItem("value.inbox", valueInbox);
}
async function encryptAndSend(plainMessage) {
const encryptedMessage = await openpgp.encrypt({
message: await openpgp.createMessage({
text: plainMessage
}),
encryptionKeys: pgpPublicKeyObj
});
let response;
try {
response = await fetch("open/post-add-message", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
user_id: userID,
message: encryptedMessage
})
});
}
catch (err) {
return false;
}
if (!response.ok) {
return false;
}
const result = await response.json();
return result.success;
}
async function tryToSendCloud() {
const valueToSend = dom.editor.value.trim();
if (valueToSend == "") {
return;
}
dom.editor.value = "";
dom.editor.focus();
dom.send.classList.add("long-hover");
window.setTimeout(() => {
dom.send.classList.remove("enabled");
dom.send.classList.remove("long-hover");
dom.send.tabIndex = -1;
}, 300);
if (dom.popups.connected.classList.contains("display")) {
dom.popups.connected.classList.remove("display");
dom.popups.sending.classList.add("display");
}
const onFailure = () => {
window.setTimeout(() => {
dom.popups.sending.classList.remove("display");
dom.popups.failure.classList.add("display");
let value = dom.editor.value.trimEnd();
if (value != "") {
value = `${value}\n\n`;
}
dom.editor.value = `${value}${valueToSend}`;
dom.send.classList.add("enabled");
dom.send.tabIndex = 0;
}, 600);
};
let success;
await delayForUX(async () => {
success = await encryptAndSend(valueToSend);
});
if (!success) {
onFailure();
return;
}
dom.popups.sending.classList.remove("display");
dom.popups.failure.classList.remove("display");
dom.popups.connected.classList.add("display");
}
function setSendLocal() {
document.body.addEventListener("keydown", event => {
if ((event.ctrlKey || event.metaKey) && !event.shiftKey && event.key.toLowerCase() == "enter") {
event.preventDefault();
tryToSendLocal();
}
});
dom.send.addEventListener("click", () => {
tryToSendLocal();
});
dom.send.addEventListener("keydown", event => {
if (
(!event.ctrlKey && !event.metaKey && !event.shiftKey && event.key.toLowerCase() == "enter")
|| event.key == " "
) {
event.preventDefault();
dom.send.click();
}
});
dom.editor.addEventListener("input", () => {
const value = dom.editor.value.trim();
if (value != "") {
dom.send.classList.add("enabled");
dom.send.classList.remove("short-hover");
dom.send.tabIndex = 0;
}
else {
dom.send.classList.remove("enabled");
dom.send.tabIndex = -1;
}
});
}
async function setSendCloud() {
dom.popups.connecting.classList.add("display");
await delayForUX(async () => {
openpgp = await import("./openpgp.min.mjs?sha1=280298d02f97111053fdb1215ac5011e0c7bd4fb");
let response;
try {
response = await fetch(`open/get-user?user_id=${encodeURIComponent(userID)}`);
}
catch (err) {
return;
}
if (!response.ok) {
return;
}
const result = await response.json();
if (!result.success) {
return;
}
pgpPublicKeyObj = await openpgp.readKey({
armoredKey: result.user.pgp_public_key
});
});
dom.popups.connecting.classList.remove("display");
if (pgpPublicKeyObj === null) {
dom.popups.disconnected.classList.add("display");
dom.editor.placeholder = "";
return;
}
dom.popups.connected.classList.add("display");
document.body.addEventListener("keydown", event => {
if ((event.ctrlKey || event.metaKey) && !event.shiftKey && event.key.toLowerCase() == "enter") {
event.preventDefault();
tryToSendCloud();
}
});
dom.send.addEventListener("click", () => {
tryToSendCloud();
});
dom.send.addEventListener("keydown", event => {
if (
(!event.ctrlKey && !event.metaKey && !event.shiftKey && event.key.toLowerCase() == "enter")
|| event.key == " "
) {
event.preventDefault();
dom.send.click();
}
});
dom.editor.addEventListener("input", () => {
const value = dom.editor.value.trim();
if (value != "") {
dom.send.classList.add("enabled");
dom.send.classList.remove("short-hover");
dom.send.tabIndex = 0;
}
else {
dom.send.classList.remove("enabled");
dom.send.tabIndex = -1;
}
});
}
async function startApp() {
const matches = window.location.hash.match(/^#([0-9a-f-]{36})(\/([^\/]*))?$/);
if (document.documentElement.getAttribute("data-location") == "local" || matches === null) {
setSendLocal();
}
else {
userID = matches[1];
const placeholderEncoded = matches[3];
if (placeholderEncoded !== undefined) {
try {
dom.editor.placeholder = decodeURIComponent(placeholderEncoded);
}
catch (err) {}
}
setPlaceholder = (text = "Send to your inbox…") => {
window.history.replaceState(null, "", `#${userID}/${encodeURIComponent(text)}`);
dom.editor.placeholder = text;
return text;
};
setSendCloud();
}
dom.send.addEventListener("mouseenter", () => {
dom.send.classList.add("short-hover");
});
dom.send.addEventListener("mouseleave", () => {
dom.send.classList.remove("short-hover");
});
window.addEventListener("storage", () => {
document.documentElement.setAttribute("data-theme", localStorage.getItem("ui.theme"));
});
}
// ******** Initial setup ********
if (localStorage.getItem("ui.theme") === null) {
localStorage.setItem("ui.theme", "system");
}
document.documentElement.setAttribute("data-theme", localStorage.getItem("ui.theme"));
const dom = {
popups: {
connecting: document.getElementById("popup-connecting"),
connected: document.getElementById("popup-connected"),
disconnected: document.getElementById("popup-disconnected"),
sending: document.getElementById("popup-sending"),
failure: document.getElementById("popup-failure")
},
editor: document.getElementById("editor"),
send: document.getElementById("send")
};
let userID = null;
let pgpPublicKeyObj = null;
let openpgp;
let setPlaceholder;
startApp();
</script>
</body>
</html>