1
+ import * as params from '@params' ;
2
+
3
+ function escapeHtml ( unsafe ) {
4
+ return unsafe
5
+ . replace ( / & / g, "&" )
6
+ . replace ( / < / g, "<" )
7
+ . replace ( / > / g, ">" )
8
+ . replace ( / " / g, """ )
9
+ . replace ( / ' / g, "'" ) ;
10
+ }
11
+ function emojify ( input , emojis ) {
12
+ let output = input ;
13
+
14
+ emojis . forEach ( emoji => {
15
+ let picture = document . createElement ( "picture" ) ;
16
+
17
+ let source = document . createElement ( "source" ) ;
18
+ source . setAttribute ( "srcset" , escapeHtml ( emoji . url ) ) ;
19
+ source . setAttribute ( "media" , "(prefers-reduced-motion: no-preference)" ) ;
20
+
21
+ let img = document . createElement ( "img" ) ;
22
+ img . className = "emoji" ;
23
+ img . setAttribute ( "src" , escapeHtml ( emoji . static_url ) ) ;
24
+ img . setAttribute ( "alt" , `:${ emoji . shortcode } :` ) ;
25
+ img . setAttribute ( "title" , `:${ emoji . shortcode } :` ) ;
26
+ img . setAttribute ( "width" , "20" ) ;
27
+ img . setAttribute ( "height" , "20" ) ;
28
+
29
+ picture . appendChild ( source ) ;
30
+ picture . appendChild ( img ) ;
31
+
32
+ output = output . replace ( `:${ emoji . shortcode } :` , picture . outerHTML ) ;
33
+ } ) ;
34
+
35
+ return output ;
36
+ }
37
+
38
+ function loadComments ( ) {
39
+ let commentsWrapper = document . getElementById ( "comments-wrapper" ) ;
40
+ document . getElementById ( "load-comment" ) . innerHTML = "Chargement" ;
41
+ fetch ( `https://${ params . host } /api/v1/statuses/${ params . id } /context` )
42
+ . then ( function ( response ) {
43
+ return response . json ( ) ;
44
+ } )
45
+ . then ( function ( data ) {
46
+ document . getElementById ( "load-comment" ) . innerHTML = "Charger les commentaires" ;
47
+
48
+ if ( data . error ) {
49
+ commentsWrapper . innerHTML = "Une erreur est survenue pendant le chargement des commentaires." ;
50
+ return ;
51
+ }
52
+
53
+ let descendants = data [ 'descendants' ] ;
54
+ if ( ! descendants || ! Array . isArray ( descendants ) || descendants . length <= 0 ) {
55
+ commentsWrapper . innerHTML = "Pas encore de commentaires." ;
56
+ return ;
57
+ }
58
+
59
+ commentsWrapper . innerHTML = "" ;
60
+
61
+ descendants . forEach ( function ( status ) {
62
+ if ( status . account . display_name . length > 0 ) {
63
+ status . account . display_name = escapeHtml ( status . account . display_name ) ;
64
+ status . account . display_name = emojify ( status . account . display_name , status . account . emojis ) ;
65
+ } else {
66
+ status . account . display_name = status . account . username ;
67
+ } ;
68
+
69
+ let instance = "" ;
70
+ if ( status . account . acct . includes ( "@" ) ) {
71
+ instance = status . account . acct . split ( "@" ) [ 1 ] ;
72
+ } else {
73
+ instance = params . host ;
74
+ }
75
+
76
+ const isReply = status . in_reply_to_id !== params . id ;
77
+
78
+ status . content = emojify ( status . content , status . emojis ) ;
79
+
80
+ let avatarSource = document . createElement ( "source" ) ;
81
+ avatarSource . setAttribute ( "srcset" , escapeHtml ( status . account . avatar ) ) ;
82
+ avatarSource . setAttribute ( "media" , "(prefers-reduced-motion: no-preference)" ) ;
83
+
84
+ let avatarImg = document . createElement ( "img" ) ;
85
+ avatarImg . className = "avatar" ;
86
+ avatarImg . setAttribute ( "src" , escapeHtml ( status . account . avatar_static ) ) ;
87
+ avatarImg . setAttribute ( "alt" , `@${ status . account . username } @${ instance } avatar` ) ;
88
+
89
+ let avatarPicture = document . createElement ( "picture" ) ;
90
+ avatarPicture . appendChild ( avatarSource ) ;
91
+ avatarPicture . appendChild ( avatarImg ) ;
92
+
93
+ let avatar = document . createElement ( "a" ) ;
94
+ avatar . className = "avatar-link" ;
95
+ avatar . setAttribute ( "href" , status . account . url ) ;
96
+ avatar . setAttribute ( "rel" , "external nofollow" ) ;
97
+ avatar . setAttribute ( "title" , `Voir le profil sur @${ status . account . username } @${ instance } ` ) ;
98
+ avatar . appendChild ( avatarPicture ) ;
99
+
100
+ let instanceBadge = document . createElement ( "a" ) ;
101
+ instanceBadge . className = "instance" ;
102
+ instanceBadge . setAttribute ( "href" , status . account . url ) ;
103
+ instanceBadge . setAttribute ( "title" , `@${ status . account . username } @${ instance } ` ) ;
104
+ instanceBadge . setAttribute ( "rel" , "external nofollow" ) ;
105
+ instanceBadge . textContent = instance ;
106
+
107
+ let display = document . createElement ( "span" ) ;
108
+ display . className = "display" ;
109
+ display . setAttribute ( "itemprop" , "author" ) ;
110
+ display . setAttribute ( "itemtype" , "http://schema.org/Person" ) ;
111
+ display . innerHTML = status . account . display_name ;
112
+
113
+ let header = document . createElement ( "header" ) ;
114
+ header . className = "author" ;
115
+ header . appendChild ( display ) ;
116
+ header . appendChild ( instanceBadge ) ;
117
+
118
+ let permalink = document . createElement ( "a" ) ;
119
+ permalink . setAttribute ( "href" , status . url ) ;
120
+ permalink . setAttribute ( "itemprop" , "url" ) ;
121
+ permalink . setAttribute ( "title" , `Voir le commentaire sur ${ instance } ` ) ;
122
+ permalink . setAttribute ( "rel" , "external nofollow" ) ;
123
+ permalink . textContent = new Date ( status . created_at ) . toLocaleString ( 'fr-FR' , {
124
+ dateStyle : "long" ,
125
+ timeStyle : "short" ,
126
+ } ) ;
127
+
128
+ let timestamp = document . createElement ( "time" ) ;
129
+ timestamp . setAttribute ( "datetime" , status . created_at ) ;
130
+ timestamp . appendChild ( permalink ) ;
131
+
132
+ let main = document . createElement ( "main" ) ;
133
+ main . setAttribute ( "itemprop" , "text" ) ;
134
+ main . innerHTML = status . content ;
135
+
136
+ let interactions = document . createElement ( "footer" ) ;
137
+ if ( status . favourites_count > 0 ) {
138
+ let faves = document . createElement ( "a" ) ;
139
+ faves . className = "faves" ;
140
+ faves . setAttribute ( "href" , `${ status . url } /favourites` ) ;
141
+ faves . setAttribute ( "title" , `Favoris depuis ${ instance } ` ) ;
142
+ faves . textContent = status . favourites_count ;
143
+
144
+ interactions . appendChild ( faves ) ;
145
+ }
146
+
147
+ let comment = document . createElement ( "article" ) ;
148
+ comment . id = `comment-${ status . id } ` ;
149
+ comment . className = isReply ? "comment comment-reply" : "comment" ;
150
+ comment . setAttribute ( "itemprop" , "comment" ) ;
151
+ comment . setAttribute ( "itemtype" , "http://schema.org/Comment" ) ;
152
+ comment . appendChild ( avatar ) ;
153
+ comment . appendChild ( header ) ;
154
+ comment . appendChild ( timestamp ) ;
155
+ comment . appendChild ( main ) ;
156
+ comment . appendChild ( interactions ) ;
157
+
158
+ commentsWrapper . innerHTML += DOMPurify . sanitize ( comment . outerHTML ) ;
159
+ } ) ;
160
+ } ) ;
161
+ }
162
+
163
+ document . getElementById ( "load-comment" ) . addEventListener ( "click" , loadComments ) ;
0 commit comments