-
Notifications
You must be signed in to change notification settings - Fork 3
/
prelude.js
208 lines (195 loc) · 7.22 KB
/
prelude.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
var randReceived = new Set();
window.addEventListener("message", async (event) => {
// console.log('eventor!',event);
event.source.postMessage('{}'); // cancel the interval in the bookmarklet
let origin = '*';
{
// we only need to parse the payload to
// 1- get the origin for postMessage (extra security, maybe unnecessary)
// 2- to get `rand`, to detect whether this is a duplicate message from the interval
const obj = JSON.parse(event.data);
if (obj.rand) {
if (randReceived.has(obj.rand)) {
// this is a duplicate message
return;
}
randReceived.add(obj.rand);
}
}
try {
const url = new URL(obj.url);
origin = url.origin;
} catch {}
document.body.append('going to post');
const res = await fetch(
'/bookmark', {method: 'POST', mode: 'cors', body: event.data, headers: {'Content-Type': 'application/json'}});
if (res.ok) {
const reply = await res.json();
if (reply && reply.htmlWanted) {
// can we avoid this round-trip JSON decode-encode?
event.source.postMessage(JSON.stringify(reply), origin);
document.body.append('… asking for HTML… ');
} else {
const button = document.createElement('button');
button.textContent = 'Close';
button.onclick = () => window.close();
// It's not clear why the `window.close()` below doesn't work if you clip things in rapid succession on Safari
// mobile so create the above button
document.body.append('… OK! You can close me!');
document.body.append(button);
window.close();
}
} else {
if (res.status === 401) {
window.location = '/auth/github';
return;
}
document.body.append('… uhoh');
const err = `${res.status} ${res.statusText}`;
console.error(err);
alert(err);
}
}, false);
window.onload = () => {
const a = document.querySelector('a#bookmarklet');
if (a && a.href) { a.href = a.href.replace(/http:\/\/localhost:3456/g, window.location.origin); }
// These special classes are implemented in renderers.ts, e.g.,
// `add-comment-button` and `comment-button`, etc.
/**
*
* @param {Event} e
*/
function handler(e) {
e.preventDefault();
/**
* @type{Element}
*/
const target = e.target;
const clickId = target.id;
const id = parseInt(clickId.slice(clickId.lastIndexOf('-') + 1));
if (isFinite(id)) {
if (target.classList.contains('add-comment-button')) {
const textarea = document.createElement('textarea');
const button = document.createElement('button');
button.innerText = 'Submit';
const div = document.createElement('div');
div.appendChild(textarea);
div.appendChild(button);
target.replaceWith(div); // replace the emoji <a> with this
button.onclick = () => {
const obj = {id, comment: textarea.value, _type: 'addCommentOnly'};
fetch('/bookmark', {method: 'POST', body: JSON.stringify(obj), headers: {'Content-Type': 'application/json'}})
.then(x => {
if (x.ok) {
location.reload();
} else {
const err = `${x.status} ${x.statusText}`;
console.error(err);
}
});
};
} else if (target.classList.contains('edit-comment-button')) {
const comment = target.parentElement.querySelector('pre.unrendered')?.textContent;
if (typeof comment !== 'string') { return; }
const textarea = document.createElement('textarea');
textarea.value = comment;
const button = document.createElement('button');
button.innerText = 'Submit';
const div = document.createElement('div');
div.appendChild(textarea);
div.appendChild(button);
target.parentElement.replaceWith(div); // replace the entire comment with this <div>
button.onclick = () => {
const obj = {content: textarea.value};
fetch('/comment/' + id,
{method: 'PUT', body: JSON.stringify(obj), headers: {'Content-Type': 'application/json'}})
.then(x => {
if (x.ok) {
location.reload();
} else {
const err = `${x.status} ${x.statusText}`;
console.error(err);
alert(err);
}
});
};
}
}
};
for (const a of document.querySelectorAll('a.comment-button')) { a.addEventListener('click', handler); }
// To create a brand new bookmark without bookmarklet
const newBookmarkButton = document.querySelector('a#add-new-bookmark');
if (newBookmarkButton) {
newBookmarkButton.addEventListener('click', e => {
e.preventDefault();
const title = document.createElement('input');
title.size = 30;
title.type = 'input';
title.id = 'new-bookmark-title';
const titleLabel = document.createElement('label');
titleLabel.append('Title? ');
titleLabel.htmlFor = title.id;
const url = document.createElement('input');
url.size = 30;
url.type = 'url';
url.id = 'new-bookmark-url';
const urlLabel = document.createElement('label');
urlLabel.append('URL? ');
urlLabel.htmlFor = url.id;
const textarea = document.createElement('textarea');
textarea.id = 'new-bookmark-comment';
const textareaLabel = document.createElement('label');
textareaLabel.append('Comment? ');
textareaLabel.htmlFor = textarea.id;
const button = document.createElement('button');
button.append('Submit');
button.onclick = e => {
const obj = {
_type: 'addBookmarkOrComment',
url: url.value,
title: title.value,
comment: textarea.value,
};
console.log(obj);
fetch('/bookmark', {method: 'POST', body: JSON.stringify(obj), headers: {'Content-Type': 'application/json'}})
.then(x => {
if (x.ok) {
location.reload();
} else {
const err = `${x.status} ${x.statusText}`;
console.error(err);
alert(err);
}
});
};
const div = document.createElement('div');
div.appendChild(titleLabel);
div.appendChild(title);
div.appendChild(document.createElement('br'));
div.appendChild(urlLabel);
div.appendChild(url);
div.appendChild(document.createElement('br'));
div.appendChild(textareaLabel);
div.appendChild(textarea);
div.appendChild(button);
e.target.replaceWith(div); // replace the emoji <a> with this
});
}
const deleteBookmarkButton = document.querySelector('.delete-bookmark button');
if (deleteBookmarkButton && deleteBookmarkButton.id) {
const fullId = deleteBookmarkButton.id;
const id = parseInt(fullId.slice(fullId.lastIndexOf('-') + 1));
if (id && isFinite(id)) {
deleteBookmarkButton.onclick = async e => {
const res = await fetch(`/bookmark/${id}`, {method: 'DELETE'});
if (res.ok) {
window.location = '/';
} else {
const err = `Error while deleting: ${res.status} ${res.statusText}`;
console.error(err);
alert(err);
}
}
}
}
};