-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHTMLGen.js
More file actions
455 lines (416 loc) · 14.3 KB
/
Copy pathHTMLGen.js
File metadata and controls
455 lines (416 loc) · 14.3 KB
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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
import { sendMessage } from "./ChatSearch";
// CSS styles for annotation elements
const annotationStyles = {
item: `
background-color: #f8f8f8;
border-left: 3px solid #6c757d;
padding: 1em;
margin-bottom: 1em;
border-radius: 2px;
font-size: 0.95em;
line-height: 1.5;
`,
content: `
margin-bottom: 0.75em;
color: #333;
`,
meta: `
font-size: 0.85em;
color: #666;
margin-bottom: 0.5em;
`,
buttonContainer: `
display: flex;
gap: 0.5em;
margin-top: 0.75em;
`,
button: `
padding: 0.4em 0.8em;
border: 1px solid #d0d0d0;
background-color: #fff;
color: #333;
border-radius: 2px;
cursor: pointer;
font-size: 0.9em;
transition: all 0.2s ease;
`,
buttonHover: `
background-color: #f0f0f0;
border-color: #999;
`,
deleteButton: `
padding: 0.4em 0.8em;
border: 1px solid #d0d0d0;
background-color: #fff;
color: #c92a2a;
border-radius: 2px;
cursor: pointer;
font-size: 0.9em;
transition: all 0.2s ease;
`,
deleteButtonHover: `
background-color: #ffe5e5;
border-color: #c92a2a;
`,
textarea: `
width: 100%;
padding: 0.75em;
border: 1px solid #d0d0d0;
border-radius: 2px;
font-family: inherit;
font-size: 0.95em;
resize: vertical;
box-sizing: border-box;
`,
};
export function createAnnotationItem(networkID, messageID, annotation, div) {
const item = document.createElement("div");
item.style.cssText = annotationStyles.item;
// {moderator} annotated user at {timestamp}: {message}
// Note: Timestamp may sometimes be a Date object and sometimes be a string depending on whether it's freshly loaded from the cache or just created, so we need to handle both cases
// If it's a String, it's a Date object that has been written as a Z encoded string, so we need to parse it back into a Date object before formatting it for display
const timestamp =
annotation.timestamp instanceof Date
? annotation.timestamp.toLocaleString()
: new Date(annotation.timestamp).toLocaleString();
const meta = document.createElement("div");
meta.style.cssText = annotationStyles.meta;
meta.textContent = `${annotation.moderator} at ${timestamp}`;
const content = document.createElement("div");
content.style.cssText = annotationStyles.content;
content.textContent = annotation.text;
const buttonContainer = document.createElement("div");
buttonContainer.style.cssText = annotationStyles.buttonContainer;
const editButton = document.createElement("button");
editButton.textContent = "Edit";
editButton.style.cssText = annotationStyles.button;
editButton.addEventListener("mouseenter", () => {
editButton.style.cssText =
annotationStyles.button + annotationStyles.buttonHover;
});
editButton.addEventListener("mouseleave", () => {
editButton.style.cssText = annotationStyles.button;
});
editButton.addEventListener("click", () => {
// Switch to edit mode, replacing the content with a textarea and a save button
const textarea = document.createElement("textarea");
textarea.value = annotation.text;
textarea.style.cssText = annotationStyles.textarea;
textarea.style.height = "5em";
const saveButton = document.createElement("button");
saveButton.textContent = "Save";
saveButton.style.cssText = annotationStyles.button;
saveButton.style.marginTop = "0.5em";
saveButton.addEventListener("mouseenter", () => {
saveButton.style.cssText =
annotationStyles.button +
annotationStyles.buttonHover +
"margin-top: 0.5em;";
});
saveButton.addEventListener("mouseleave", () => {
saveButton.style.cssText = annotationStyles.button + "margin-top: 0.5em;";
});
const cancelEditButton = document.createElement("button");
cancelEditButton.textContent = "Cancel";
cancelEditButton.style.cssText =
annotationStyles.button + "margin-top: 0.5em; margin-left: 0.5em;";
cancelEditButton.addEventListener("mouseenter", () => {
cancelEditButton.style.cssText =
annotationStyles.button +
annotationStyles.buttonHover +
"margin-top: 0.5em; margin-left: 0.5em;";
});
cancelEditButton.addEventListener("mouseleave", () => {
cancelEditButton.style.cssText =
annotationStyles.button + "margin-top: 0.5em; margin-left: 0.5em;";
});
saveButton.addEventListener("click", () => {
// Send an edit command to the chat with the new annotation text
const command = `AN${networkID} EDIT(${messageID}): ${textarea.value}`;
sendMessage(command, GM_getValue("fkey"));
// Update the item and switch back to view mode
annotation.text = textarea.value;
content.textContent = annotation.text;
textarea.remove();
saveButton.remove();
cancelEditButton.remove();
buttonContainer.style.display = "flex";
});
cancelEditButton.addEventListener("click", () => {
// Cancel editing, remove textarea, saveButton, and cancelEditButton
textarea.remove();
saveButton.remove();
cancelEditButton.remove();
buttonContainer.style.display = "flex";
});
// Create a button group container for save and cancel buttons
const editButtonContainer = document.createElement("div");
editButtonContainer.style.cssText = annotationStyles.buttonContainer;
editButtonContainer.appendChild(saveButton);
editButtonContainer.appendChild(cancelEditButton);
// Hide buttons and show textarea
buttonContainer.style.display = "none";
item.appendChild(textarea);
item.appendChild(editButtonContainer);
});
const deleteButton = document.createElement("button");
deleteButton.textContent = "Delete";
deleteButton.style.cssText = annotationStyles.deleteButton;
deleteButton.addEventListener("mouseenter", () => {
deleteButton.style.cssText =
annotationStyles.deleteButton + annotationStyles.deleteButtonHover;
});
deleteButton.addEventListener("mouseleave", () => {
deleteButton.style.cssText = annotationStyles.deleteButton;
});
deleteButton.addEventListener("click", () => {
// Confirm the deletion with the user, getting an optional reason for the deletion
const reason = prompt(
"Are you sure you want to delete this annotation? This action cannot be undone. You can optionally provide a reason for the deletion:"
);
if (reason !== null) {
// Send a delete command to the chat with the annotation ID
const command = `AN${networkID} UNDO(${messageID}): ${
reason || "<no reason provided>"
}`;
sendMessage(command, GM_getValue("fkey"));
// Remove the item from the DOM immediately
item.remove();
}
});
div.appendChild(item);
item.appendChild(meta);
item.appendChild(content);
item.appendChild(buttonContainer);
buttonContainer.appendChild(editButton);
buttonContainer.appendChild(deleteButton);
}
export function createClearCacheButton(networkID) {
const userIDRegex = /\/users\/(\d+)\//g.exec(document.location);
if (!userIDRegex) return;
const userID = userIDRegex[1];
const moderatorLinkElement = $("a[data-se-mod-button-id=" + userID + "]");
if (!moderatorLinkElement.length) return;
const button = document.createElement("button");
button.textContent = "Clear Annotation Cache for This User";
button.style.cssText = `
padding: 0.4em 0.8em;
border: 1px solid #d0d0d0;
background-color: #fff;
color: #333;
border-radius: 2px;
cursor: pointer;
font-size: 0.9em;
margin-left: 0.5em;
transition: all 0.2s ease;
`;
button.addEventListener("mouseenter", () => {
button.style.backgroundColor = "#f0f0f0";
button.style.borderColor = "#999";
});
button.addEventListener("mouseleave", () => {
button.style.backgroundColor = "#fff";
button.style.borderColor = "#d0d0d0";
});
button.addEventListener("click", (e) => {
e.preventDefault();
if (
confirm(
"Are you sure you want to clear the annotation cache for this user?"
)
) {
const annotations = JSON.parse(GM_getValue("annotations"));
if (annotations[networkID]) {
delete annotations[networkID];
GM_setValue("annotations", JSON.stringify(annotations));
alert(
"Annotation cache cleared for this user. Please refresh the page to see the changes."
);
} else {
alert("No annotation cache found for this user.");
}
}
});
moderatorLinkElement.after(button);
}
export function createAnnotationsDiv() {
const targetDiv = document.querySelector(".js-user-header");
const annotationsDiv = document.createElement("div");
annotationsDiv.id = "se-annotations";
annotationsDiv.style.cssText = `
background-color: #fff;
border: 1px solid #e0e0e0;
border-radius: 3px;
margin-top: 2em;
margin-bottom: 2em;
padding: 0;
box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05);
`;
const title = document.createElement("h2");
title.textContent = "Profile Annotations";
title.style.cssText = `
color: #222;
padding: 1em 1em 0.5em 1em;
margin: 0;
font-size: 1.2em;
font-weight: 600;
border-bottom: 1px solid #f0f0f0;
`;
annotationsDiv.appendChild(title);
// Add padding container for annotations
const contentContainer = document.createElement("div");
contentContainer.style.cssText = `
padding: 1em;
`;
annotationsDiv.appendChild(contentContainer);
// Replace the direct children access with the container
const originalAppendChild = annotationsDiv.appendChild.bind(annotationsDiv);
annotationsDiv.appendChild = function (child) {
if (
child !== title &&
child !== contentContainer &&
child.tagName !== "DIV"
) {
contentContainer.appendChild(child);
} else if (child !== title && child !== contentContainer) {
contentContainer.appendChild(child);
}
return child;
};
annotationsDiv._contentContainer = contentContainer;
targetDiv.after(annotationsDiv);
return annotationsDiv;
}
export function createAnnotationButton(networkID, annotationsDiv) {
const button = document.createElement("button");
button.textContent = "+ Add";
button.style.cssText = `
padding: 0.4em 0.8em;
border: 1px solid #d0d0d0;
background-color: #fff;
color: #333;
border-radius: 2px;
cursor: pointer;
font-size: 0.9em;
margin-left: auto;
transition: all 0.2s ease;
`;
button.addEventListener("mouseenter", () => {
button.style.backgroundColor = "#f0f0f0";
button.style.borderColor = "#999";
});
button.addEventListener("mouseleave", () => {
button.style.backgroundColor = "#fff";
button.style.borderColor = "#d0d0d0";
});
button.addEventListener("click", () => {
// Put a textarea and a submit button below the user header for adding a new annotation
const textarea = document.createElement("textarea");
textarea.style.cssText = `
width: 100%;
padding: 0.75em;
border: 1px solid #d0d0d0;
border-radius: 2px;
font-family: inherit;
font-size: 0.95em;
resize: vertical;
box-sizing: border-box;
height: 5em;
`;
textarea.placeholder = "Enter annotation text here...";
const submitButton = document.createElement("button");
submitButton.textContent = "Submit";
submitButton.style.cssText = `
padding: 0.4em 0.8em;
border: 1px solid #d0d0d0;
background-color: #fff;
color: #333;
border-radius: 2px;
cursor: pointer;
font-size: 0.9em;
margin-top: 0.5em;
transition: all 0.2s ease;
`;
submitButton.addEventListener("mouseenter", () => {
submitButton.style.backgroundColor = "#f0f0f0";
submitButton.style.borderColor = "#999";
});
submitButton.addEventListener("mouseleave", () => {
submitButton.style.backgroundColor = "#fff";
submitButton.style.borderColor = "#d0d0d0";
});
const cancelButton = document.createElement("button");
cancelButton.textContent = "Cancel";
cancelButton.style.cssText = `
padding: 0.4em 0.8em;
border: 1px solid #d0d0d0;
background-color: #fff;
color: #333;
border-radius: 2px;
cursor: pointer;
font-size: 0.9em;
margin-top: 0.5em;
margin-left: 0.5em;
transition: all 0.2s ease;
`;
cancelButton.addEventListener("mouseenter", () => {
cancelButton.style.backgroundColor = "#f0f0f0";
cancelButton.style.borderColor = "#999";
});
cancelButton.addEventListener("mouseleave", () => {
cancelButton.style.backgroundColor = "#fff";
cancelButton.style.borderColor = "#d0d0d0";
});
cancelButton.addEventListener("click", () => {
// Remove the textarea, submit button, and cancel button
textarea.remove();
submitButton.remove();
cancelButton.remove();
button.style.display = "block";
});
submitButton.addEventListener("click", async function () {
if (textarea.value.trim()) {
const command = `AN${networkID}: ${textarea.value}`;
const newID = await sendMessage(command, GM_getValue("fkey"));
// Remove the textarea, submit button, and cancel button after submitting the annotation
textarea.remove();
submitButton.remove();
cancelButton.remove();
button.style.display = "block";
// Add the new annotation to the annotationsDiv immediately
const container = annotationsDiv._contentContainer || annotationsDiv;
const newAnnotation = {
text: textarea.value,
timestamp: new Date(),
moderator: "You",
commandID: newID,
};
createAnnotationItem(networkID, newID, newAnnotation, container);
}
});
const buttonContainer = document.createElement("div");
buttonContainer.style.cssText = `
display: flex;
gap: 0.5em;
margin-top: 0.5em;
`;
buttonContainer.appendChild(submitButton);
buttonContainer.appendChild(cancelButton);
const container = annotationsDiv._contentContainer || annotationsDiv;
container.appendChild(textarea);
container.appendChild(buttonContainer);
button.style.display = "none";
});
const titleElement = annotationsDiv.querySelector("h2");
titleElement.style.cssText = `
display: flex;
align-items: center;
color: #222;
padding: 1em 1em 0.5em 1em;
margin: 0;
font-size: 1.2em;
font-weight: 600;
border-bottom: 1px solid #f0f0f0;
`;
titleElement.appendChild(button);
}