1+ export default async function ( { feature, console } ) {
2+ window . feature = feature
3+
4+ let pinned = await ( await fetch ( `https://data.scratchtools.app/pinned/${ feature . redux . getState ( ) . preview . projectInfo . id } /` ) ) . json ( )
5+ let { username : author } = feature . redux . getState ( ) . preview . projectInfo . author
6+ let { id } = feature . redux . getState ( ) . preview . projectInfo
7+ let { username } = feature . redux . getState ( ) . session ?. session ?. user
8+
9+ let AUTHOR = author
10+
11+ if ( pinned . commentId ) {
12+ ScratchTools . waitForElements ( ".comments-list" , async function ( list ) {
13+ let data = await ( await fetch ( `https://api.scratch.mit.edu/users/${ pinned . author } /` ) ) . json ( )
14+ if ( data ) {
15+ let box = document . createElement ( "div" )
16+ box . className = "ste-pinned-comment"
17+
18+ let h3 = document . createElement ( "h3" )
19+ h3 . textContent = "Pinned Comment"
20+ box . appendChild ( h3 )
21+
22+ let comment = document . createElement ( "div" )
23+ comment . className = "flex-row comment-container"
24+ box . appendChild ( comment )
25+
26+ let inner = document . createElement ( "div" )
27+ inner . className = "flex-row comment"
28+ comment . appendChild ( inner )
29+
30+ let a = document . createElement ( "a" )
31+ a . href = `/users/${ pinned . author } /`
32+ inner . appendChild ( a )
33+
34+ let img = document . createElement ( "img" )
35+ img . src = data . profile . images [ "90x90" ]
36+ img . className = "avatar"
37+ a . appendChild ( img )
38+
39+ let main = document . createElement ( "div" )
40+ main . className = "flex-row comment-body column"
41+ inner . appendChild ( main )
42+
43+ let topRow = document . createElement ( "div" )
44+ topRow . className = "flex-row comment-top-row"
45+ main . appendChild ( topRow )
46+
47+ let author = document . createElement ( "a" )
48+ author . href = `/users/${ pinned . author } /`
49+ author . className = "username"
50+ author . textContent = data . username
51+ topRow . appendChild ( author )
52+
53+ let bubble = document . createElement ( "div" )
54+ bubble . className = "comment-bubble"
55+ main . appendChild ( bubble )
56+
57+ let content = document . createElement ( "span" )
58+ content . textContent = pinned . content
59+ content . className = "comment-content"
60+ bubble . appendChild ( content )
61+
62+ let bottomRow = document . createElement ( "div" )
63+ bottomRow . className = "flex-row comment-bottom-row"
64+ bubble . appendChild ( bottomRow )
65+
66+ let time = document . createElement ( "span" )
67+ time . className = "comment-time"
68+ time . textContent = `Comment pinned by @${ AUTHOR } .`
69+ bottomRow . appendChild ( time )
70+
71+ let goTo = document . createElement ( "span" )
72+ goTo . className = "comment-goto"
73+ bottomRow . appendChild ( goTo )
74+
75+ let goToLink = document . createElement ( "a" )
76+ goToLink . href = `https://scratch.mit.edu/projects/${ pinned . projectId } /#comments-${ pinned . commentId } `
77+ goToLink . textContent = "view full comment"
78+ goTo . appendChild ( goToLink )
79+
80+ list . parentElement . insertBefore ( box , list )
81+ }
82+ } )
83+ }
84+
85+ if ( author === username ) {
86+ ScratchTools . waitForElements ( "div.flex-row.comments-list > div > div.flex-row.comment" , function ( comment ) {
87+ let actions = comment . querySelector ( "div.action-list" )
88+ if ( ! actions || actions . querySelector ( ".comment-pin" ) ) return ;
89+
90+ if ( pinned . commentId ?. toString ( ) === comment . id . replace ( "comments-" , "" ) ) {
91+ let span = document . createElement ( "span" )
92+ span . className = "comment-pin"
93+ let innerSpan = document . createElement ( "span" )
94+ innerSpan . textContent = "Unpin"
95+ span . appendChild ( innerSpan )
96+ actions . prepend ( span )
97+
98+ span . addEventListener ( "click" , async function ( ) {
99+ ScratchTools . verifyUser ( async function ( token ) {
100+ let data = await (
101+ await fetch ( "https://data.scratchtools.app/unpin/" , {
102+ method : "POST" ,
103+ headers : {
104+ Accept : "application/json" ,
105+ "Content-Type" : "application/json" ,
106+ } ,
107+ body : JSON . stringify ( { token, project : id . toString ( ) } ) ,
108+ } )
109+ ) . json ( ) ;
110+
111+ if ( data . error ) {
112+ ScratchTools . modals . create ( {
113+ title : "Error" ,
114+ description :
115+ data . error ,
116+ } ) ;
117+ } else {
118+ ScratchTools . modals . create ( {
119+ title : "Comment Unpinned" ,
120+ description :
121+ "This comment has been unpinned. You may reload the page to see the change." ,
122+ } ) ;
123+ }
124+ } )
125+ } )
126+ } else {
127+ let span = document . createElement ( "span" )
128+ span . className = "comment-pin"
129+ let innerSpan = document . createElement ( "span" )
130+ innerSpan . textContent = "Pin"
131+ span . appendChild ( innerSpan )
132+ actions . prepend ( span )
133+
134+ span . addEventListener ( "click" , async function ( ) {
135+ ScratchTools . verifyUser ( async function ( token ) {
136+ let data = await (
137+ await fetch ( "https://data.scratchtools.app/pin/" , {
138+ method : "POST" ,
139+ headers : {
140+ Accept : "application/json" ,
141+ "Content-Type" : "application/json" ,
142+ } ,
143+ body : JSON . stringify ( { token, project : id . toString ( ) , author : comment . querySelector ( "a" ) . href . split ( "/" ) [ 4 ] , id : comment . id . replace ( "comments-" , "" ) , content : comment . querySelector ( ".comment-content" ) . textContent } ) ,
144+ } )
145+ ) . json ( ) ;
146+
147+ if ( data . error ) {
148+ ScratchTools . modals . create ( {
149+ title : "Error" ,
150+ description :
151+ data . error ,
152+ } ) ;
153+ } else {
154+ ScratchTools . modals . create ( {
155+ title : "Comment Pinned" ,
156+ description :
157+ "This comment has been pinned and you may reload the page to see. Please only pin appropriate comments. If the comment you pinned is not appropriate, it may be removed and you may lose the ability to pin comments in the future." ,
158+ } ) ;
159+ }
160+ } )
161+ } )
162+ }
163+ } )
164+ }
165+ }
0 commit comments