This repository has been archived by the owner on Jan 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
388 lines (343 loc) · 14.3 KB
/
index.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
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
// Beakermentions (index.js) - Read and send responses on what you're
// currently viewing on Beaker Browser.
//
// Written in 2020 by Don Geronimo <email@sentamal.in>
//
// To the extent possible under law, the author(s) have dedicated all copyright
// and related and neighboring rights to this software to the public domain
// worldwide. This software is distributed without any warranty.
//
// You should have received a copy of the CC0 Public Domain Dedication along
// with this software. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
import { File } from "./modules/File.js";
import { WebmentionValidator } from "./modules/Validator/index.js";
import { Configuration } from "./modules/Configuration.js";
import * as WindowMessages from "./modules/WindowMessages.js";
let currentFile;
let configuration;
// Disabled as Iframe window messaging is currently not working
// const endpointIframe = document.getElementById("endpoint-iframe").contentWindow;
let endpointURL;
let endpointReady;
let targetURL;
let paneURL = null;
let pendingURL = null;
const domParser = new DOMParser();
const validator = new WebmentionValidator({ domParser: domParser });
async function main() {
// Load configuration from local storage and update configuration views
configuration = new Configuration(window.localStorage, {
darkMode: window.localStorage.getItem("darkMode"),
endpoint: window.localStorage.getItem("endpoint"),
isProfile: window.localStorage.getItem("isProfile"),
driveURL: window.localStorage.getItem("driveURL"),
useWindowMessage: window.localStorage.getItem("useWindowMessage")
});
updateAllConfigurationViews();
// Set event handlers for configuration changes
configuration.onDarkModeSet(response => { updateDarkModeView(response); });
configuration.onEndpointSet(response => { updateEndpointView(response); });
configuration.onIsProfileSet(response => { updateDriveButtonView(response); });
configuration.onDriveURLSet(response => { updateDriveInfoView(response); });
// Set event handlers for configuration view interactions
document.getElementById("config-endpoint").addEventListener("keyup", onEndpointChange);
document.getElementById("config-drive-profile").addEventListener("click", onProfileButtonClick);
document.getElementById("config-drive-drive").addEventListener("click", onDriveButtonClick);
document.getElementById("app-settings").addEventListener("click", onConfigurationAnchorClick);
document.getElementById("config-theme-light").addEventListener("click", onLightModeClick);
document.getElementById("config-theme-dark").addEventListener("click", onDarkModeClick);
// Set event handlers for <iframe> messages
window.addEventListener("message", async event => {
const message = JSON.parse(event.data);
// Verify that the event's origin is the endpoint that you expect before doing anything
if (event.origin === endpointURL.origin) {
switch (message.type) {
case "handshake":
endpointIframe.postMessage(JSON.stringify(WindowMessages.sendOrigin()), endpointURL.origin);
console.debug("Handshake Received:", message);
break;
case "ready":
if (message.origin === location.origin) {
isEndpointReady(true);
console.debug("Ready Received:", message);
console.debug("Endpoint Ready is:", endpointReady);
}
break;
case "webmentions":
console.debug("Webmentions Received:", message);
break;
case "success":
pendingURL = null;
loadMentions(paneURL);
console.debug("Success Received:", message);
break;
case "failure":
if (pendingURL) {
await beaker.hyperdrive.unlink(pendingURL);
pendingURL = null;
}
console.debug("Failure Received:", message);
break;
}
}
// If it's not, show an error of some sort
else {
console.error("Message received from <iframe>, but was not from the expected origin.");
}
}, false);
// Set up loading the current page from the last attached pane
beaker.panes.setAttachable();
let pane = await beaker.panes.attachToLastActivePane();
if (pane) {
paneURL = pane.url;
targetURL = paneURL;
}
beaker.panes.addEventListener("pane-navigated", e => {
paneURL = e.detail.url;
targetURL = paneURL;
loadMentions(e.detail.url);
});
if (paneURL) { loadMentions(paneURL); }
}
/********** Updating Configuration Views **********/
function updateAllConfigurationViews() {
updateDarkModeView(configuration.darkMode);
updateEndpointView(configuration.endpoint);
updateDriveButtonView(configuration.isProfile);
updateDriveInfoView(configuration.driveURL);
}
function updateDarkModeView(response) {
// Change the highlighted style button
if (response) {
document.getElementById("config-theme-light").classList.remove("current");
document.getElementById("config-theme-dark").classList.add("current");
} else {
document.getElementById("config-theme-light").classList.add("current");
document.getElementById("config-theme-dark").classList.remove("current");
}
// Change the stylesheet
if (response) { document.querySelector("link[title='page']").setAttribute("href", "dark.css"); }
else { document.querySelector("link[title='page']").setAttribute("href", "light.css"); }
}
function updateEndpointView(response) {
if (response) { document.getElementById("config-endpoint").value = response; }
}
function updateDriveButtonView(response) {
if (response) {
document.getElementById("config-drive-profile").classList.add("current");
document.getElementById("config-drive-drive").classList.remove("current");
} else {
document.getElementById("config-drive-profile").classList.remove("current");
document.getElementById("config-drive-drive").classList.add("current");
}
}
async function updateDriveInfoView(response) {
if (response) {
try {
const driveInfo = await beaker.hyperdrive.getInfo(response);
document.getElementById("config-drive-info").textContent = driveInfo.title;
} catch {}
}
}
/********** Browser Events **********/
// Configuration Events
function onConfigurationAnchorClick() {
const configPage = document.getElementById("config");
if (configPage.classList.contains("hidden")) { configPage.classList.remove("hidden"); }
else { configPage.classList.add("hidden"); }
}
function onLightModeClick() {
configuration.darkMode = false;
}
function onDarkModeClick() {
configuration.darkMode = true;
}
async function onProfileButtonClick() {
try {
let profile = await beaker.contacts.requestProfile();
configuration.isProfile = true;
configuration.driveURL = profile.url;
} catch {}
}
async function onDriveButtonClick() {
try {
let drive = await beaker.shell.selectDriveDialog({
writable: true
});
configuration.isProfile = false;
configuration.driveURL = drive;
} catch {}
}
function onEndpointChange() {
configuration.endpoint = document.getElementById("config-endpoint").value;
}
// Mention Loading Events
function onLoadingMentions() {
document.getElementById("mentions").classList.add("hidden");
document.getElementById("blank").classList.add("hidden");
document.getElementById("no-endpoint").classList.add("hidden");
document.getElementById("no-replies").classList.add("hidden");
document.getElementById("loading").classList.remove("hidden");
document.getElementById("mentions").textContent = "";
document.getElementById("file-like-total").textContent = 0;
document.getElementById("file-repost-total").textContent = 0;
}
function onMentionsLoaded() {
appendMentions(currentFile, document.querySelector("#mentions"), document.querySelector("#mention-template"))
document.getElementById("loading").classList.add("hidden");
document.getElementById("mentions").classList.remove("hidden");
}
function onNoEndpoint() {
document.getElementById("loading").classList.add("hidden");
document.getElementById("no-endpoint").classList.remove("hidden");
}
function onNoReplies() {
document.getElementById("loading").classList.add("hidden");
document.getElementById("no-replies").classList.remove("hidden");
}
/********** Load and append mentions **********/
async function loadMentions(url) {
currentFile = new File({
"url" : url,
"validator" : validator,
"domParser" : domParser
});
onLoadingMentions();
await currentFile.init();
document.getElementById("file-like-total").textContent = currentFile.totalLikes;
document.getElementById("file-repost-total").textContent = currentFile.totalReposts;
if (currentFile.endpoint) { setEndpoint(currentFile.endpoint); }
if (!currentFile.endpoint) { onNoEndpoint(); }
else if (!currentFile.mentions.length) { onNoReplies(); }
else { onMentionsLoaded(); }
}
function appendMentions(file, container, template) {
for(let i = 0; i < file.mentions.length; i++) {
if (file.mentions[i].isAReply) {
let clone = template.content.cloneNode(true);
clone.querySelector(".mention-url > h1").textContent = file.mentions[i].author;
clone.querySelector(".mention-meta > img").setAttribute("src", file.mentions[i].thumb);
clone.querySelector(".mention-like-total").textContent = file.mentions[i].totalLikes;
clone.querySelector(".mention-repost-total").textContent = file.mentions[i].totalReposts;
clone.querySelector(".mention-url").setAttribute("href", file.mentions[i].url);
switch (file.mentions[i].content.type) {
case "text":
clone.querySelector(".mention-content").innerHTML = `<p>${file.mentions[i].content.content}</p>`;
break;
case "html":
clone.querySelector(".mention-content").innerHTML = file.mentions[i].content.content;
break;
case "image":
clone.querySelector(".mention-content").innerHTML = `<img src="${file.mentions[i].content.url}" loading="lazy" />`;
clone.querySelector(".mention-content").classList.add("visual-media");
clone.querySelector("footer").classList.add("visual-media");
break;
case "file":
clone.querySelector(".mention-content").innerHTML = `<p>Linked a <a href="${file.mentions[i].content.url}">file</a>.</p>`;
break;
}
appendMentions(file.mentions[i], clone.querySelector(".nested-mentions"), template);
container.appendChild(clone);
}
}
}
/********** <iframe>-setup functions **********/
function setEndpoint(endpoint) {
if (((!endpointURL) || (endpoint !== endpointURL.href)) && configuration.useWindowMessage) {
endpointURL = new URL(endpoint);
endpointIframe.location.href = endpointURL;
isEndpointReady(false);
} else { isEndpointReady(true); }
}
function isEndpointReady(ready) {
if (ready) {
endpointReady = true;
if (configuration.driveURL) {
document.getElementById("sender-text").removeAttribute("disabled");
document.getElementById("sender-send").removeAttribute("disabled");
document.getElementById("sender-attach").removeAttribute("disabled");
document.getElementById("sender-send").addEventListener("click", sendMarkdownMention);
document.getElementById("sender-attach").addEventListener("click", sendFileMention);
}
} else {
endpointReady = false;
document.getElementById("sender-text").setAttribute("disabled", "disabled");
document.getElementById("sender-send").setAttribute("disabled", "disabled");
document.getElementById("sender-attach").setAttribute("disabled", "disabled");
document.getElementById("sender-send").removeEventListener("click", sendMarkdownMention);
document.getElementById("sender-attach").removeEventListener("click", sendFileMention);
}
}
/********** Mention-sending functions **********/
async function sendMarkdownMention() {
const markdown = document.getElementById("sender-text").value;
const now = new Date();
const path = `/beaker/beakermentions/${now.getTime()}.md`;
if (endpointReady && configuration.driveURL) {
const drive = beaker.hyperdrive.drive(configuration.driveURL);
await drive.writeFile(path, markdown, "utf8");
let metadata = {
"inReplyTo" : targetURL
}
if (configuration.endpoint) { metadata.webmention = configuration.endpoint; }
await drive.updateMetadata(path, metadata);
const source = new URL(path, configuration.driveURL);
pendingURL = source.href;
await sendSendMessage(source.href, targetURL);
}
}
async function sendFileMention() {
/* STUB: Need to use File API to get uploaded files from user
const upload = "Sample Uploaded File";
const now = new Date();
const path = `/beaker/beakermentions/samplepath.thing`;
if (endpointReady && configuration.driveURL) {
const drive = beaker.hyperdrive.drive(configuration.driveURL);
await drive.writeFile(path, upload, "binary");
let metadata = {
"inReplyTo" : targetURL
}
if (configuration.endpoint) { metadata.webmention = configuration.endpoint; }
await drive.updateMetadata(path, metadata);
const source = new URL(path, configuration.driveURL);
pendingURL = source.href;
await sendSendMessage(source.href, targetURL);
} */
let selectedFile = await beaker.shell.selectFileDialog({
title: "Select a file to add to your response",
buttonLabel: "Select File",
select: ["file"],
allowMultiple: false,
disallowCreate: false
});
const fileExtension = selectedFile[0].path.split(".").pop();
let markdown;
switch (fileExtension) {
case "png":
case "jpg":
case "jpeg":
case "gif":
case "svg":
markdown = `![Image's alternative text](${selectedFile[0].url})`;
break;
default:
const selectedFilename = selectedFile[0].path.split("/").pop();
markdown = `[${selectedFilename}](${selectedFile[0].url})`;
break;
}
document.getElementById("sender-text").value += markdown;
}
async function sendSendMessage(source, target) {
if (configuration.useWindowMessage) {
endpointIframe.postMessage(JSON.stringify(WindowMessages.sendWebmention(source, target)), endpointURL.origin);
} else {
let targetEndpoint = await validator.getTargetEndpoint(target);
let request = new URL(targetEndpoint);
request.search = `?source=${source}&target=${target}&done=${location.origin}`;
location.href = request;
}
}
function sendGetMessage(target) {
endpointIframe.postMessage(JSON.stringify(WindowMessages.getWebmentions(target)), endpointURL.origin);
}
main();