@@ -115,18 +115,25 @@ async function handleNewNode(
115115 }
116116
117117 let anchor ;
118- let card ;
119- let tweetText ;
120118
121- const linkPreview = tryLinkPreview ( element ) ;
119+ const linkPreview = findLinkPreview ( element ) ;
120+
121+ let container = findContainerInTweet (
122+ linkPreview ?. card ?? element ,
123+ Boolean ( linkPreview ) ,
124+ ) ;
122125 if ( linkPreview ) {
123126 anchor = linkPreview . anchor ;
124- card = linkPreview . card ;
127+ container ?. remove ( ) ;
128+ container = linkPreview . card . parentElement as HTMLElement ;
125129 } else {
126- const link = tryLinkInText ( element ) ;
130+ if ( container ) {
131+ return ;
132+ }
133+ const link = findLastLinkInText ( element ) ;
127134 if ( link ) {
128135 anchor = link . anchor ;
129- tweetText = link . tweetText ;
136+ container = getContainerForLink ( link . tweetText ) ;
130137 }
131138 }
132139
@@ -175,22 +182,13 @@ async function handleNewNode(
175182 return ;
176183 }
177184
178- //double check if link preview appeared after we assumed it is not there
179- const isCardPresent = findCardInTweet ( element ) ;
180- if ( ! card && isCardPresent ) {
181- console . log ( 'found card in tweet' ) ;
182- return ;
183- }
184-
185185 const action = await Action . fetch ( actionApiUrl , config ) . catch ( ( ) => null ) ;
186186
187187 if ( ! action ) {
188188 return ;
189189 }
190190
191- const container = card ? card . parentElement : getContainerForLink ( tweetText ! ) ;
192-
193- container ?. replaceChildren (
191+ addMargin ( container ) ?. replaceChildren (
194192 createAction ( {
195193 originalUrl : actionUrl ,
196194 action,
@@ -259,29 +257,19 @@ function findElementByTestId(element: Element, testId: string) {
259257 return element . querySelector ( `[data-testid="${ testId } "]` ) ;
260258}
261259
262- function getContainerForLink ( tweetText : Element ) {
263- const root = document . createElement ( 'div' ) ;
264- root . style . paddingTop = '12px' ;
265- const dm = tweetText . closest ( `[data-testid="messageEntry"]` ) ;
266- if ( dm ) {
267- tweetText . parentElement ?. parentElement ?. prepend ( root ) ;
268- root . style . paddingBottom = '8px' ;
269- } else {
270- tweetText . parentElement ?. append ( root ) ;
271- }
272- return root ;
273- }
260+ function findContainerInTweet ( element : Element , searchUp ?: boolean ) {
261+ const message = searchUp
262+ ? element . closest ( `[data-testid="tweet"]` ) ??
263+ element . closest ( `[data-testid="messageEntry"]` )
264+ : findElementByTestId ( element , 'tweet' ) ??
265+ findElementByTestId ( element , 'messageEntry' ) ;
274266
275- function findCardInTweet ( element : Element ) {
276- const message =
277- findElementByTestId ( element , 'tweet' ) ??
278- findElementByTestId ( element , 'messageEntry' ) ;
279267 if ( message ) {
280- return findElementByTestId ( message , 'card. wrapper') ;
268+ return message . querySelector ( '.dialect- wrapper') as HTMLElement ;
281269 }
282270}
283271
284- function tryLinkPreview ( element : Element ) {
272+ function findLinkPreview ( element : Element ) {
285273 const card = findElementByTestId ( element , 'card.wrapper' ) ;
286274 if ( card ) {
287275 const linkPreview = card . children [ 0 ] ;
@@ -291,17 +279,38 @@ function tryLinkPreview(element: Element) {
291279 }
292280 }
293281}
294- function tryLinkInText ( element : Element ) {
282+ function findLastLinkInText ( element : Element ) {
295283 const tweetText = findElementByTestId ( element , 'tweetText' ) ;
296- if ( ! tweetText || tweetText . classList . contains ( 'dialect-link-tweet' ) ) {
284+ if ( ! tweetText ) {
297285 return ;
298286 }
299287
300288 const links = tweetText . getElementsByTagName ( 'a' ) ;
301289 if ( links . length > 0 ) {
302- //marking tweet as visited
303- tweetText . classList . add ( 'dialect-link-tweet' ) ;
304290 const anchor = links [ links . length - 1 ] as HTMLAnchorElement ;
305291 return { anchor, tweetText } ;
306292 }
307293}
294+
295+ function getContainerForLink ( tweetText : Element ) {
296+ const root = document . createElement ( 'div' ) ;
297+ root . className = 'dialect-wrapper' ;
298+ const dm = tweetText . closest ( `[data-testid="messageEntry"]` ) ;
299+ if ( dm ) {
300+ root . classList . add ( 'dialect-dm' ) ;
301+ tweetText . parentElement ?. parentElement ?. prepend ( root ) ;
302+ } else {
303+ tweetText . parentElement ?. append ( root ) ;
304+ }
305+ return root ;
306+ }
307+
308+ function addMargin ( element ?: HTMLElement ) {
309+ if ( element && element . classList . contains ( 'dialect-wrapper' ) ) {
310+ element . style . marginTop = '12px' ;
311+ if ( element . classList . contains ( 'dialect-dm' ) ) {
312+ element . style . marginBottom = '8px' ;
313+ }
314+ }
315+ return element ;
316+ }
0 commit comments