-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmastodon-timeline.js
810 lines (729 loc) · 23.9 KB
/
mastodon-timeline.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
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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
/**
* Mastodon embed feed timeline v3.10.0
* More info at:
* https://gitlab.com/idotj/mastodon-embed-feed-timeline
*/
/**
* Timeline settings
* Adjust these parameters to customize your timeline
*/
window.addEventListener("load", () => {
const mastodonTimeline = new MastodonApi({
// Id of the <div> containing the timeline
container_body_id: "mt-body",
// Class name for the loading spinner (also used in CSS file)
spinner_class: "loading-spinner",
// Preferred color theme: 'light', 'dark' or 'auto'. Default: auto
default_theme: "light",
// Your Mastodon instance
instance_url: "https://mastodon.social",
// Choose type of toots to show in the timeline: 'local', 'profile', 'hashtag'. Default: local
timeline_type: "profile",
// Your user ID on Mastodon instance. Leave empty if you didn't choose 'profile' as type of timeline
user_id: "111245589313877734",
// Your user name on Mastodon instance. Leave empty if you didn't choose 'profile' as type of timeline
profile_name: "@[email protected]",
// The name of the hashtag. Leave empty if you didn't choose 'hashtag' as type of timeline
hashtag_name: "",
// Maximum amount of toots to get. Default: 20
toots_limit: "20",
// Hide unlisted toots. Default: don't hide
hide_unlisted: false,
// Hide boosted toots. Default: don't hide
hide_reblog: false,
// Hide replies toots. Default: don't hide
hide_replies: false,
// Hide preview card if toot contains a link, photo or video from a URL. Default: don't hide
hide_preview_link: false,
// Hide custom emojis available on the server. Default: don't hide
hide_emojos: false,
// Converts Markdown symbol ">" at the beginning of a paragraph into a blockquote HTML tag. Ddefault: don't apply
markdown_blockquote: false,
// Hide replies, boosts and favourites toots counter. Default: don't hide
hide_counter_bar: false,
// Limit the text content to a maximum number of lines. Default: 0 (unlimited)
text_max_lines: "0",
// Customize the text of the link pointing to the Mastodon page (appears after the last toot)
link_see_more: "See more posts at Mastodon",
});
});
/**
* Set all variables with customized values or use default ones
* @param {object} params_ User customized values
* Trigger main function to build the timeline
*/
const MastodonApi = function (params_) {
this.CONTAINER_BODY_ID = params_.container_body_id || "mt-body";
this.SPINNER_CLASS = params_.spinner_class || "loading-spinner";
this.DEFAULT_THEME = params_.default_theme || "auto";
this.INSTANCE_URL = params_.instance_url;
this.USER_ID = params_.user_id || "";
this.PROFILE_NAME = this.USER_ID ? params_.profile_name : "";
this.TIMELINE_TYPE = params_.timeline_type || "local";
this.HASHTAG_NAME = params_.hashtag_name || "";
this.TOOTS_LIMIT = params_.toots_limit || "20";
this.HIDE_UNLISTED =
typeof params_.hide_unlisted !== "undefined"
? params_.hide_unlisted
: false;
this.HIDE_REBLOG =
typeof params_.hide_reblog !== "undefined" ? params_.hide_reblog : false;
this.HIDE_REPLIES =
typeof params_.hide_replies !== "undefined" ? params_.hide_replies : false;
this.HIDE_PREVIEW_LINK =
typeof params_.hide_preview_link !== "undefined"
? params_.hide_preview_link
: false;
this.HIDE_EMOJOS =
typeof params_.hide_emojos !== "undefined" ? params_.hide_emojos : false;
this.MARKDOWN_BLOCKQUOTE =
typeof params_.markdown_blockquote !== "undefined"
? params_.markdown_blockquote
: false;
this.HIDE_COUNTER_BAR =
params_.hide_counter_bar !== "undefined" ? params_.hide_counter_bar : false;
this.TEXT_MAX_LINES = params_.text_max_lines || "0";
this.LINK_SEE_MORE = params_.link_see_more;
this.FETCHED_DATA = {};
this.mtBodyContainer = document.getElementById(this.CONTAINER_BODY_ID);
this.buildTimeline();
};
/**
* Trigger functions and construct timeline
*/
MastodonApi.prototype.buildTimeline = async function () {
// Apply color theme
this.setTheme();
// Get server data
await this.getTimelineData();
// Empty the <div> container
this.mtBodyContainer.innerHTML = "";
for (let i in this.FETCHED_DATA.timeline) {
// First filter (Public / Unlisted)
if (
this.FETCHED_DATA.timeline[i].visibility == "public" ||
(!this.HIDE_UNLISTED &&
this.FETCHED_DATA.timeline[i].visibility == "unlisted")
) {
// Second filter (Reblog / Replies)
if (
(this.HIDE_REBLOG && this.FETCHED_DATA.timeline[i].reblog) ||
(this.HIDE_REPLIES && this.FETCHED_DATA.timeline[i].in_reply_to_id)
) {
// Nothing here (Don't append toots)
} else {
// Append toots
this.appendToot(this.FETCHED_DATA.timeline[i], Number(i));
}
}
}
// Check if there are toots in the container (due to filters applied)
if (this.mtBodyContainer.innerHTML === "") {
this.mtBodyContainer.setAttribute("role", "none");
this.mtBodyContainer.innerHTML =
'<div class="mt-error"><span class="mt-error-icon">📭</span><br/><strong>Sorry, no toots to show</strong><br/><div class="mt-error-message">Got ' +
this.FETCHED_DATA.timeline.length +
' toots from the server but due to the "hide filters" applied, no toot is shown</div></div>';
} else {
// Insert link after last toot to visit Mastodon page
if (this.LINK_SEE_MORE) {
let linkSeeMorePath = "";
if (this.TIMELINE_TYPE === "profile") {
linkSeeMorePath = this.PROFILE_NAME;
} else if (this.TIMELINE_TYPE === "hashtag") {
linkSeeMorePath = "tags/" + this.HASHTAG_NAME;
} else if (this.TIMELINE_TYPE === "local") {
linkSeeMorePath = "public/local";
}
let linkSeeMore =
'<div class="mt-footer"><a href="' +
this.INSTANCE_URL +
"/" +
linkSeeMorePath +
'" target="_blank" rel="nofollow noopener noreferrer">' +
this.LINK_SEE_MORE +
"</a></div>";
this.mtBodyContainer.parentNode.insertAdjacentHTML(
"beforeend",
linkSeeMore
);
}
// Control loading spinners
this.manageSpinner();
}
// Toot interactions
this.mtBodyContainer.addEventListener("click", function (e) {
// Check if toot cointainer was clicked
if (
e.target.localName == "article" ||
e.target.offsetParent?.localName == "article" ||
e.target.localName == "img"
) {
openTootURL(e);
}
// Check if Show More/Less button was clicked
if (e.target.localName == "button" && e.target.className == "spoiler-btn") {
toogleSpoiler(e);
}
});
this.mtBodyContainer.addEventListener("keydown", function (e) {
// Check if Enter key was pressed with focus in an article
if (e.key === "Enter" && e.target.localName == "article") {
openTootURL(e);
}
});
/**
* Open toot in a new page avoiding any other natural link
* @param {event} e User interaction trigger
*/
const openTootURL = function (e) {
let urlToot = e.target.closest(".mt-toot").dataset.location;
if (
e.target.localName !== "a" &&
e.target.localName !== "span" &&
e.target.localName !== "button" &&
e.target.localName !== "time" &&
e.target.className !== "mt-toot-preview-noImage" &&
e.target.parentNode.className !== "mt-toot-avatar-image-big" &&
e.target.parentNode.className !== "mt-toot-avatar-image-small" &&
e.target.parentNode.className !== "mt-toot-preview-image" &&
urlToot
) {
window.open(urlToot, "_blank");
}
};
/**
* Spoiler button
* @param {event} e User interaction trigger
*/
const toogleSpoiler = function (e) {
const nextSibling = e.target.nextSibling;
if (nextSibling.localName === "img") {
e.target.parentNode.classList.remove("mt-toot-media-spoiler");
e.target.style.display = "none";
} else if (
nextSibling.classList.contains("spoiler-text-hidden") ||
nextSibling.classList.contains("spoiler-text-visible")
) {
if (e.target.textContent == "Show more") {
nextSibling.classList.remove("spoiler-text-hidden");
nextSibling.classList.add("spoiler-text-visible");
e.target.setAttribute("aria-expanded", "true");
e.target.textContent = "Show less";
} else {
nextSibling.classList.remove("spoiler-text-visible");
nextSibling.classList.add("spoiler-text-hidden");
e.target.setAttribute("aria-expanded", "false");
e.target.textContent = "Show more";
}
}
};
};
/**
* Set the theme style chosen by the user or by the browser/OS
*/
MastodonApi.prototype.setTheme = function () {
/**
* Set the theme value in the <html> tag using the attribute "data-theme"
* @param {string} theme Type of theme to apply: dark or light
*/
const setTheme = function (theme) {
document.documentElement.setAttribute("data-theme", theme);
};
if (this.DEFAULT_THEME === "auto") {
let systemTheme = window.matchMedia("(prefers-color-scheme: dark)");
systemTheme.matches ? setTheme("dark") : setTheme("light");
// Update the theme if user change browser/OS preference
systemTheme.addEventListener("change", (e) => {
e.matches ? setTheme("dark") : setTheme("light");
});
} else {
setTheme(this.DEFAULT_THEME);
}
};
/**
* Requests to the server to get all the data
*/
MastodonApi.prototype.getTimelineData = async function () {
return new Promise((resolve, reject) => {
/**
* Fetch data from server
* @param {string} url address to fetch
* @returns {object} List of objects
*/
async function fetchData(url) {
const response = await fetch(url);
if (!response.ok) {
throw new Error(
"Failed to fetch the following URL: " +
url +
"<hr>" +
"Error status: " +
response.status +
"<hr>" +
"Error message: " +
response.statusText
);
}
const data = await response.json();
return data;
}
// URLs to fetch
let urls = {};
if (this.TIMELINE_TYPE === "profile") {
urls.timeline = `${this.INSTANCE_URL}/api/v1/accounts/${this.USER_ID}/statuses?limit=${this.TOOTS_LIMIT}`;
} else if (this.TIMELINE_TYPE === "hashtag") {
urls.timeline = `${this.INSTANCE_URL}/api/v1/timelines/tag/${this.HASHTAG_NAME}?limit=${this.TOOTS_LIMIT}`;
} else if (this.TIMELINE_TYPE === "local") {
urls.timeline = `${this.INSTANCE_URL}/api/v1/timelines/public?local=true&limit=${this.TOOTS_LIMIT}`;
}
if (!this.HIDE_EMOJOS) {
urls.emojos = this.INSTANCE_URL + "/api/v1/custom_emojis";
}
const urlsPromises = Object.entries(urls).map(([key, url]) => {
return fetchData(url)
.then((data) => ({ [key]: data }))
.catch((error) => {
reject(new Error("Something went wrong fetching data"));
this.mtBodyContainer.innerHTML =
'<div class="mt-error"><span class="mt-error-icon">❌</span><br/><strong>Sorry, request failed:</strong><br/><div class="mt-error-message">' +
error.message +
"</div></div>";
this.mtBodyContainer.setAttribute("role", "none");
return { [key]: [] };
});
});
// Fetch all urls simultaneously
Promise.all(urlsPromises).then((dataObjects) => {
this.FETCHED_DATA = dataObjects.reduce((result, dataItem) => {
return { ...result, ...dataItem };
}, {});
// console.log("Timeline data fetched: ", this.FETCHED_DATA);
resolve();
});
});
};
/**
* Inner function to add each toot in timeline container
* @param {object} c Toot content
* @param {number} i Index of toot
*/
MastodonApi.prototype.appendToot = function (c, i) {
this.mtBodyContainer.insertAdjacentHTML("beforeend", this.assambleToot(c, i));
};
/**
* Build toot structure
* @param {object} c Toot content
* @param {number} i Index of toot
*/
MastodonApi.prototype.assambleToot = function (c, i) {
let avatar, user, userName, url, date, formattedDate;
if (c.reblog) {
// BOOSTED toot
// Toot url
url = c.reblog.url;
// Boosted avatar
avatar =
'<a href="' +
c.reblog.account.url +
'" class="mt-toot-avatar" rel="nofollow noopener noreferrer" target="_blank">' +
'<div class="mt-toot-avatar-boosted">' +
'<div class="mt-toot-avatar-image-big">' +
'<img src="' +
c.reblog.account.avatar +
'" alt="' +
c.reblog.account.username +
' avatar" loading="lazy" />' +
"</div>" +
'<div class="mt-toot-avatar-image-small">' +
'<img src="' +
c.account.avatar +
'" alt="' +
c.account.username +
' avatar" loading="lazy" />' +
"</div>" +
"</div>" +
"</a>";
// User name and url
userName = this.showEmojos(
c.reblog.account.display_name
? c.reblog.account.display_name
: c.reblog.account.username,
this.FETCHED_DATA.emojos
);
user =
'<div class="mt-toot-header-user">' +
'<a href="' +
c.reblog.account.url +
'" rel="nofollow noopener noreferrer" target="_blank">' +
userName +
'<span class="visually-hidden"> account</span>' +
"</a>" +
"</div>";
// Date
date = c.reblog.created_at;
} else {
// STANDARD toot
// Toot url
url = c.url;
// Avatar
avatar =
'<a href="' +
c.account.url +
'" class="mt-toot-avatar" rel="nofollow noopener noreferrer" target="_blank">' +
'<div class="mt-toot-avatar-standard">' +
'<div class="mt-toot-avatar-image-big">' +
'<img src="' +
c.account.avatar +
'" alt="' +
c.account.username +
' avatar" loading="lazy" />' +
"</div>" +
"</div>" +
"</a>";
// User name and url
userName = this.showEmojos(
c.account.display_name ? c.account.display_name : c.account.username,
this.FETCHED_DATA.emojos
);
user =
'<div class="mt-toot-header-user">' +
'<a href="' +
c.account.url +
'" rel="nofollow noopener noreferrer" target="_blank">' +
userName +
'<span class="visually-hidden"> account</span>' +
"</a>" +
"</div>";
// Date
date = c.created_at;
}
// Date
formattedDate = this.formatDate(date);
let timestamp =
'<div class="mt-toot-header-date">' +
'<a href="' +
url +
'" rel="nofollow noopener noreferrer" target="_blank">' +
'<time datetime="' +
date +
'">' +
formattedDate +
"</time>" +
"</a>" +
"</div>";
// Main text
let text_css = "";
if (this.TEXT_MAX_LINES !== "0") {
text_css = "truncate";
document.documentElement.style.setProperty(
"--text-max-lines",
this.TEXT_MAX_LINES
);
}
let content = "";
if (c.spoiler_text !== "") {
content =
'<div class="mt-toot-text">' +
c.spoiler_text +
' <button type="button" class="spoiler-btn" aria-expanded="false">Show more</button>' +
'<div class="spoiler-text-hidden">' +
this.formatTootText(c.content) +
"</div>" +
"</div>";
} else if (
c.reblog &&
c.reblog.content !== "" &&
c.reblog.spoiler_text !== ""
) {
content =
'<div class="mt-toot-text">' +
c.reblog.spoiler_text +
' <button type="button" class="spoiler-btn" aria-expanded="false">Show more</button>' +
'<div class="spoiler-text-hidden">' +
this.formatTootText(c.reblog.content) +
"</div>" +
"</div>";
} else if (
c.reblog &&
c.reblog.content !== "" &&
c.reblog.spoiler_text === ""
) {
content =
'<div class="mt-toot-text' +
text_css +
'">' +
'<div class="mt-toot-text-wrapper">' +
this.formatTootText(c.reblog.content) +
"</div>" +
"</div>";
} else {
content =
'<div class="mt-toot-text' +
text_css +
'">' +
'<div class="mt-toot-text-wrapper">' +
this.formatTootText(c.content) +
"</div>" +
"</div>";
}
// Media attachments
let media = "";
if (c.media_attachments.length > 0) {
for (let picid in c.media_attachments) {
media = this.placeMedias(c.media_attachments[picid], c.sensitive);
}
}
if (c.reblog && c.reblog.media_attachments.length > 0) {
for (let picid in c.reblog.media_attachments) {
media = this.placeMedias(
c.reblog.media_attachments[picid],
c.reblog.sensitive
);
}
}
// Preview link
let previewLink = "";
if (!this.HIDE_PREVIEW_LINK && c.card) {
previewLink = this.placePreviewLink(c.card);
}
// Poll
let poll = "";
let pollOption = "";
if (c.poll) {
for (let i in c.poll.options) {
pollOption += "<li>" + c.poll.options[i].title + "</li>";
}
poll =
'<div class="mt-toot-poll">' + "<ul>" + pollOption + "</ul>" + "</div>";
}
// Counter bar
let counterBar = "";
if (!this.HIDE_COUNTER_BAR) {
let repliesCount =
'<div class="mt-toot-counter-bar-replies">' +
'<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1792 1600" aria-hidden="true"><path d="M1792 1056q0 166-127 451q-3 7-10.5 24t-13.5 30t-13 22q-12 17-28 17q-15 0-23.5-10t-8.5-25q0-9 2.5-26.5t2.5-23.5q5-68 5-123q0-101-17.5-181t-48.5-138.5t-80-101t-105.5-69.5t-133-42.5t-154-21.5t-175.5-6H640v256q0 26-19 45t-45 19t-45-19L19 621Q0 602 0 576t19-45L531 19q19-19 45-19t45 19t19 45v256h224q713 0 875 403q53 134 53 333z"/></svg>' +
c.replies_count +
"</div>";
let reblogCount =
'<div class="mt-toot-counter-bar-reblog">' +
'<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1920 1280" aria-hidden="true"><path d="M1280 1248q0 13-9.5 22.5t-22.5 9.5H288q-8 0-13.5-2t-9-7t-5.5-8t-3-11.5t-1-11.5V640H64q-26 0-45-19T0 576q0-24 15-41l320-384q19-22 49-22t49 22l320 384q15 17 15 41q0 26-19 45t-45 19H512v384h576q16 0 25 11l160 192q7 10 7 21zm640-416q0 24-15 41l-320 384q-20 23-49 23t-49-23l-320-384q-15-17-15-41q0-26 19-45t45-19h192V384H832q-16 0-25-12L647 180q-7-9-7-20q0-13 9.5-22.5T672 128h960q8 0 13.5 2t9 7t5.5 8t3 11.5t1 11.5v600h192q26 0 45 19t19 45z"/></svg>' +
c.reblogs_count +
"</div>";
let favoritesCount =
'<div class="mt-toot-counter-bar-favorites">' +
'<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1664 1600" aria-hidden="true"><path d="M1664 615q0 22-26 48l-363 354l86 500q1 7 1 20q0 21-10.5 35.5T1321 1587q-19 0-40-12l-449-236l-449 236q-22 12-40 12q-21 0-31.5-14.5T301 1537q0-6 2-20l86-500L25 663Q0 636 0 615q0-37 56-46l502-73L783 41q19-41 49-41t49 41l225 455l502 73q56 9 56 46z"/></svg>' +
c.favourites_count +
"</div>";
counterBar =
'<div class="mt-toot-counter-bar">' +
repliesCount +
reblogCount +
favoritesCount +
"</div>";
}
// Add all to main toot container
let toot =
'<article class="mt-toot" aria-posinset="' +
(i + 1) +
'" aria-setsize="' +
this.TOOTS_LIMIT +
'" data-location="' +
url +
'" tabindex="0">' +
'<div class="mt-toot-header">' +
avatar +
user +
timestamp +
"</div>" +
content +
media +
previewLink +
poll +
counterBar +
"</article>";
return toot;
};
/**
* Handle text changes made to toots
* @param {string} c Text content
* @returns {string} Text content modified
*/
MastodonApi.prototype.formatTootText = function (c) {
let content = c;
// Format hashtags and mentions
content = this.addTarget2hashtagMention(content);
// Convert emojos shortcode into images
if (!this.HIDE_EMOJOS) {
content = this.showEmojos(content, this.FETCHED_DATA.emojos);
}
// Convert markdown styles into HTML
if (this.MARKDOWN_BLOCKQUOTE) {
content = this.replaceHTMLtag(
content,
"<p>>",
"</p>",
"<blockquote><p>",
"</p></blockquote>"
);
}
return content;
};
/**
* Add target="_blank" to all #hashtags and @mentions in the toot
* @param {string} c Text content
* @returns {string} Text content modified
*/
MastodonApi.prototype.addTarget2hashtagMention = function (c) {
let content = c.replaceAll('rel="tag"', 'rel="tag" target="_blank"');
content = content.replaceAll(
'class="u-url mention"',
'class="u-url mention" target="_blank"'
);
return content;
};
/**
* Find all custom emojis shortcode and replace by image
* @param {string} c Text content
* @param {array} e List with all custom emojis
* @returns {string} Text content modified
*/
MastodonApi.prototype.showEmojos = function (c, e) {
if (c.includes(":")) {
for (const emojo of e) {
const regex = new RegExp(`\\:${emojo.shortcode}\\:`, "g");
c = c.replace(
regex,
`<img src="${emojo.url}" class="custom-emoji" alt="Emoji ${emojo.shortcode}" />`
);
}
return c;
} else {
return c;
}
};
/**
* Find all start/end <tags> and replace them by another start/end <tags>
* @param {string} c Text content
* @param {string} initialTagOpen Start HTML tag to replace
* @param {string} initialTagClose End HTML tag to replace
* @param {string} replacedTagOpen New start HTML tag
* @param {string} replacedTagClose New end HTML tag
* @returns {string} Text in HTML format
*/
MastodonApi.prototype.replaceHTMLtag = function (
c,
initialTagOpen,
initialTagClose,
replacedTagOpen,
replacedTagClose
) {
if (c.includes(initialTagOpen)) {
const regex = new RegExp(initialTagOpen + "(.*?)" + initialTagClose, "gi");
return c.replace(regex, replacedTagOpen + "$1" + replacedTagClose);
} else {
return c;
}
};
/**
* Place media
* @param {object} m Media content
* @param {boolean} s Spoiler/Sensitive status
* @returns {string} Media in HTML format
*/
MastodonApi.prototype.placeMedias = function (m, s) {
let spoiler = s || false;
const pic =
'<div class="mt-toot-media img-ratio14_7 ' +
(spoiler ? "mt-toot-media-spoiler " : "") +
this.SPINNER_CLASS +
'">' +
(spoiler ? '<button class="spoiler-btn">Show content</button>' : "") +
'<img src="' +
m.preview_url +
'" alt="' +
(m.description ? m.description : "") +
'" loading="lazy" />' +
"</div>";
return pic;
};
/**
* Place preview link
* @param {object} c Preview link content
* @returns {string} Preview link in HTML format
*/
MastodonApi.prototype.placePreviewLink = function (c) {
let card =
'<a href="' +
c.url +
'" class="mt-toot-preview" target="_blank" rel="noopener noreferrer">' +
(c.image
? '<div class="mt-toot-preview-image ' +
this.SPINNER_CLASS +
'"><img src="' +
c.image +
'" alt="" loading="lazy" /></div>'
: '<div class="mt-toot-preview-noImage">📄</div>') +
"</div>" +
'<div class="mt-toot-preview-content">' +
(c.provider_name
? '<span class="mt-toot-preview-provider">' + c.provider_name + "</span>"
: "") +
'<span class="mt-toot-preview-title">' +
c.title +
"</span>" +
(c.author_name
? '<span class="mt-toot-preview-author">By ' + c.author_name + "</span>"
: "") +
"</div>" +
"</a>";
return card;
};
/**
* Format date
* @param {string} d Date in ISO format (YYYY-MM-DDTHH:mm:ss.sssZ)
* @returns {string} Date formated (MM DD, YYYY)
*/
MastodonApi.prototype.formatDate = function (d) {
const monthNames = [
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
"Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec",
];
let date = new Date(d);
const displayDate =
monthNames[date.getMonth()] +
" " +
date.getDate() +
", " +
date.getFullYear();
return displayDate;
};
/**
* Add/Remove event listener for loading spinner
*/
MastodonApi.prototype.manageSpinner = function () {
// Remove CSS class to container and listener to images
const spinnerCSS = this.SPINNER_CLASS;
const removeSpinner = function () {
this.parentNode.classList.remove(spinnerCSS);
this.removeEventListener("load", removeSpinner);
this.removeEventListener("error", removeSpinner);
};
// Add listener to images
this.mtBodyContainer
.querySelectorAll(`.${this.SPINNER_CLASS} > img`)
.forEach((e) => {
e.addEventListener("load", removeSpinner);
e.addEventListener("error", removeSpinner);
});
};